---
title: "Multiple Morbidities"
author: "Jonas Prenissl"
date: "5/29/2018"
output: word_document
---
```{r Input Analysis in merged dataset}



# Data cleaning and merging
library(tidyverse) 
library(dplyr) 
library(forcats) # for categorical variables (R for data science rec) --> see https://rdrr.io/cran/forcats/man/fct_unify.html
library(stringr) # for manipulating string variables (R for data science rec)
#library(lubridate) # for dates and times (R for data science rec)
#instalibrary(dummies) # to easily create dummies
library(ggplot2) 
library(ggrepel) # to avoid text labels in ggplot from overlapping
library(modelr) # to use "add_predictions()" for adding a column of predicted vals to your dataset
library(broom) # to create tidy data from model output
#library(margins) # R equivalent of Stata's margins command --> Thomas Leeper said this only to be used for marginal effects (not prediction)
#library(prediction) # Thomas Leeper's R package to get predicted probabilities
library(srvyr)  # survey package that also works with dplyr 
library(lmtest) # for likelihood ratio tests
#library(sandwich) # for robust standard errors 
#library(multiwayvcov) # for clustered standard errors
library(miceadds) # package to cluster SEs more easily than in multiwayvcov; it uses multiwayvcov, so the results between the two packages are exactly the same. 
library(speedglm)
library(foreign) # for importing non-csv datasets
library(readstata13) # foreign only reads Stata 12 or lower
#library(lme4) # for multi-level modeling
#library(lmerTest) # for p-values with the lmer command
#library(sjPlot) # for plotting lmer models
#library(texreg) # for tables
library(tableone) # Creates a table 1 (summary characteristics)
#library(mice) # md.pattern() function to see patterns of missing data 
#library(reshape) # to use the rescalar function

#library(car) # for easy attaching of new variables
#library(arm)
#library(mosaic)
#library(mosaicData)
library(mediation)  # for mediation analysis
#library(lattice)
#library(pander)
library(haven)
#install.packages("eulerr")
library(eulerr)
library(UpSetR)
library(RColorBrewer)
library(clusterSEs)
library(sandwich)
library(multiwayvcov)
library(miceadds)








##Krankheiten::

#AHS & DLHS & DHS: 
  
#  obesity
#diab
#hypertension
#underweight
#smoking
#smokeless tobacco


#DHS only:
  
# hiv
#asthma
#anemia

`DLHS4&AHS&DHS.merged(15.05.2018)` <- read.csv("~/iCloud Drive (Archive)/Documents/Public Health Files/Public Health/Multiple Morbidities/Datasets/merg with correct smoke for dhs.csv")

merg <- `DLHS4&AHS&DHS.merged(15.05.2018)`



setwd("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity")


####new anemia variable

merg <- dplyr::mutate(merg,
                           mild_anemia = ifelse((sex = 1 & ex_hb_ind < 12 & ex_hb_ind >= 11) | (sex = 0 & ex_hb_ind < 13 &ex_hb_ind >= 11 ), 1, 0))
merg$mild_anemia <- factor(merg$mild_anemia, levels = c("0", "1"))

merg <- dplyr::mutate(merg,
                            moderate_anemia = ifelse((sex = 1 & ex_hb_ind < 11 & ex_hb_ind >= 8) | (sex = 0 & ex_hb_ind < 11 & ex_hb_ind >= 8 ), 1, 0))
merg$moderate_anemia <- factor(merg$moderate_anemia, levels = c("0", "1"))

merg <- dplyr::mutate(merg,
                            severe_anemia = ifelse((sex = 1 & ex_hb_ind < 8) | (sex = 0 & ex_hb_ind < 8 ), 1, 0))
merg$severe_anemia <- factor(merg$severe_anemia, levels = c("0", "1")) 


merg <- mutate(merg, 
               ex_anemia_ind = ifelse(moderate_anemia==1 | severe_anemia==1,1,0))

length(which(merg$ex_anemia_ind==1))

#####create correct morbiditiy categories
merg <- mutate(merg, 
               sev_underweight = ifelse(bmi<16,1,0))

merg <- mutate(merg, 
               obese = ifelse(bmi>=27.5,1,0))

######make new smoking variable since csmoke is incorrect for dhs

merg <- mutate(merg, 
                     smoke = ifelse(csmoke==1 & svy=="AHS", 1, 
                                    ifelse(csmoke==1 & svy=="DLHS",1,
                                           ifelse(tobacco_smoked==1 & svy=="DHS",1,0))))



######FILTER out those that have missing values for risc factors


merg <- filter(merg, is.na(merg$obese)==F & is.na(merg$ex_diab_narrow_ind)==F & is.na(merg$ex_htn_narrow_ind)==F )





#####create correct characteristics

merg <- dplyr::mutate(merg, age_grp = ifelse(age<=24 , "15-24", 
                                                             ifelse(age>24 &  age<=34, "25-34",
                                                                    ifelse(age>34 &  age<=44, "35-44",
                                                                           ifelse(age>44 &  age<=54, "45-54", 
                                                                                  ifelse(age>54 &  age<=64, "55-64",
                                                                                         ifelse(age>64, "65+", NA)))))))

                                                                              
merg$age_grp <- factor(merg$age_grp, levels = c("15-24", "25-34", "35-44", "45-54","55-64", "65+"))
merg <- within(merg, age_grp <- relevel(age_grp, ref = "15-24"))


merg <- dplyr::mutate(merg, age_grp_old = ifelse(age<=25 , "15-25", 
                                                             ifelse(age>25 &  age<=35, "26-35",
                                                                    ifelse(age>35 &  age<=45, "36-45",
                                                                           ifelse(age>45 &  age<=55, "46-55", 
                                                                                  ifelse(age>55 &  age<=65, "56-65",
                                                                                         ifelse(age>65 &  age<=75, "66-75",
                                                                                         ifelse(age>75, "76+", NA))))))))

                                                                              
merg$age_grp_old <- factor(merg$age_grp_old, levels = c("15-25", "26-35", "36-45", "46-55","56-65", "66-75", "76+"))
merg <- within(merg, age_grp_old <- relevel(age_grp_old, ref = "15-25"))



merg <- mutate(merg, 
                     rural = ifelse(urban==1, 0, 1),
                     female = ifelse(sex==1, 1, 0),
                     male = ifelse(sex==1, 0, 1))
merg$rural <- as.factor(merg$rural)
merg$female <- as.factor(merg$female)
merg$male <- as.factor(merg$male)

merg <- dplyr::mutate(merg, educatnames = ifelse(educat_lcl==0, "No formal education", 
                                                             ifelse(educat_lcl==1, "< Primary school",
                                                                    ifelse(educat_lcl==2, "Primary school",
                                                                           ifelse(educat_lcl==3, "Middle school",
                                                                                  ifelse(educat_lcl==4, "Secondary school",
                                                                                          ifelse(educat_lcl==5, "> Secondary school",NA))))))) 
merg$educatnames <- factor(merg$educatnames, levels = c("No formal education", "< Primary school", "Primary school", "Middle school", "Secondary school", "> Secondary school", NA))
merg <- within(merg, educatnames<- relevel(educatnames, ref = "No formal education"))


merg <- dplyr::mutate(merg, educatnames_few = ifelse(educat_lcl==0 | educat_lcl==1, "< Primary school", 
                                                            ifelse(educat_lcl==2 | educat_lcl==3, "< Secondary school",
                                                                           ifelse(educat_lcl==4, "Secondary school",
                                                                                  ifelse(educat_lcl==5, "> Secondary school",NA))))) 
merg$educatnames_few <- factor(merg$educatnames_few, levels = c("< Primary school", "< Secondary school", "Secondary school", "> Secondary school", NA))
merg <- within(merg, educatnames_few<- relevel(educatnames_few, ref = "< Primary school"))




merg <- merg %>% 
  mutate(female_lab = as.factor(ifelse(is.na(female) == TRUE, NA, 
                                       ifelse(female == 1, "Female", "Male"))),
         urban_lab = as.factor(ifelse(is.na(rural) == TRUE, NA, 
                                      ifelse(rural == 1, "Rural", "Urban"))),
         wealth_quintile_rurb_lab = as.factor(ifelse(is.na(wealth_quintile_rurb) == TRUE, NA, 
                                                     ifelse( wealth_quintile_rurb == 1, "Q1 (Poorest)", 
                                                             ifelse(wealth_quintile_rurb == 2, "Q2",
                                                                    ifelse(wealth_quintile_rurb == 3, "Q3",
                                                                           ifelse(wealth_quintile_rurb == 4, "Q4",
                                                                                  ifelse(wealth_quintile_rurb == 5, "Q5 (Richest)", NA))))))))

merg$wealth_quintile_rurb_lab <- as.factor(merg$wealth_quintile_rurb_lab)
merg<- within(merg, wealth_quintile_rurb_lab <- relevel(wealth_quintile_rurb_lab, ref = "Q1 (Poorest)"))

merg$obese <- as.factor(merg$obese)
merg$csmoke <- as.factor(merg$csmoke)
merg$ex_htn_narrow_ind <- as.factor(merg$ex_htn_narrow_ind)
merg$ex_diab_narrow_ind <- as.factor(merg$ex_diab_narrow_ind)
merg$sev_underweight <- as.factor(merg$sev_underweight)
merg$ex_anemia_ind <- as.factor(merg$ex_anemia_ind)
merg$educat <- as.factor(merg$educat)

#######make numeric morbidities where NA is 0

merg <- mutate(merg, 
               anemia = ifelse(ex_anemia_ind==1,1,0))
merg <- mutate(merg, 
               csmkls_tb = ifelse(csmkls_tb==1,1,0))
merg <- mutate(merg, 
              anemia_dbl = as.numeric(anemia),
              obese_dbl = as.numeric(obese)-1,
              sev_underweight_dbl = as.numeric(sev_underweight)-1,
              ex_diab_narrow_ind_dbl = as.numeric(ex_diab_narrow_ind)-1,
              ex_htn_narrow_ind_dbl = as.numeric(ex_htn_narrow_ind)-1,
              smoke_dbl = as.numeric(smoke),
              csmkls_tb_dbl = as.numeric(csmkls_tb))

summary(merg$obese_dbl)
summary(merg$sev_underweight_dbl)
summary(merg$ex_diab_narrow_ind_dbl)
summary(merg$ex_htn_narrow_ind_dbl)
summary(merg$smoke_dbl)
summary(merg$csmkls_tb_dbl)

#####make NAs to 0 in dbl

merg[which(is.na(merg$anemia_dbl)==T), "anemia_dbl"]<-0

merg[which(is.na(merg$smoke_dbl)==T), "smoke_dbl"]<-0

merg[which(is.na(merg$ex_diab_narrow_ind_dbl)==T), "ex_diab_narrow_ind_dbl"]<-0
merg[which(is.na(merg$ex_htn_narrow_ind_dbl)==T), "ex_htn_narrow_ind_dbl"]<-0
merg[which(is.na(merg$obese_dbl)==T), "obese_dbl"]<-0
merg[which(is.na(merg$sev_underweight_dbl)==T), "sev_underweight_dbl"]<-0
merg[which(is.na(merg$csmkls_tb_dbl)==T), "csmkls_tb_dbl"]<-0





#####make CVD morb factor dummy

merg<- mutate(merg, 
              sum_CVD_morb = obese_dbl  + ex_diab_narrow_ind_dbl + ex_htn_narrow_ind_dbl )     
merg <- mutate(merg, 
               sum_CVD_morb_dbl = as.numeric(sum_CVD_morb))

merg <- mutate(merg,
               multi_CVD_morb = ifelse(sum_CVD_morb>=2,1,0))

merg <- mutate(merg, 
               multi_CVD_morb_dbl = as.numeric(multi_CVD_morb))

merg$multi_CVD_morb <- as.factor(merg$multi_CVD_morb)
merg$sum_CVD_morb <- as.factor(merg$sum_CVD_morb)

summary(merg$multi_CVD_morb)
summary(merg$sum_CVD_morb)


summary(merg$obese)
summary(merg$sev_underweight)
summary(merg$ex_diab_narrow_ind)
summary(merg$ex_htn_narrow_ind)
summary(merg$smoke)
summary(merg$csmkls_tb)




#####Number of persons with one of these diseases

length(which(merg$ex_anemia_ind==1 | merg$obese==1 | merg$ex_diab_narrow_ind==1| merg$ex_htn_narrow_ind==1 | merg$sev_underweight==1 | merg$csmoke==1)) ###992251



#######make interaction dummies

#merg <- merg %>% 
##  mutate( 
#    Diabetes_Hypertension = ifelse(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1,1,0),
#    Diabetes_Anemia = ifelse(merg$ex_diab_narrow_ind==1& merg$ex_anemia_ind==1,1,0),
#    Obese_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$obese==1,1,0),
#    Smoking_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$smoke==1,1,0),
#    sev_Thinness_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$sev_underweight==1,1,0),    Hypertension_Anemia= ifelse(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1,1,0),
 #   Obese_Hypertension= ifelse(merg$ex_htn_narrow_ind==1& merg$obese==1,1,0),
#    sev_Thinness_Hypertension= ifelse(merg$ex_htn_narrow_ind==1& merg$sev_underweight==1,1,0),
 #   Smoking_Hypertension= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1,1,0),
 #   Obese_Anemia= ifelse(merg$ex_anemia_ind==1& merg$obese==1,1,0),
 #   Obese_Smoking= ifelse(merg$smoke==1& merg$obese==1,1,0),
 #   Smoking_sev_Thinness= ifelse(merg$sev_underweight==1& merg$smoke==1,1,0),
#    Anemia_sev_Thinness= ifelse(merg$sev_underweight==1& merg$ex_anemia_ind==1,1,0),
##    Anemia_Smoking= ifelse(merg$smoke==1& merg$ex_anemia_ind==1,1,0),
 #   Smoking_Hypertension_Diabetes= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$ex_diab_narrow_ind==1,1,0),
 #   Smoking_Hypertension_Obese= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$obese==1,1,0),
 #   Smoking_Hypertension_anemia= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$ex_anemia_ind==1,1,0),
 #   Smoking_Hypertension_sev_Thinness= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$sev_underweight==1,1,0),
 #   Smoking_Diabetes_Obese= ifelse(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$obese==1,1,0),
 #   Smoking_sev_Thinness_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$ex_anemia_ind==1,1,0),
  #  Smoking_Diabetes_sev_Thinness= ifelse(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$sev_underweight==1,1,0),
  #  Obese_Diabetes_Hypertension= ifelse(merg$ex_diab_narrow_ind==1& merg$obese==1 & merg$ex_htn_narrow_ind==1,1,0),
  #  Obese_sev_Thinness_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$obese==1 & merg$ex_anemia_ind==1,1,0),
  #  Obese_Anemia_Smoking= ifelse(merg$ex_anemia_ind==1& merg$obese==1 & merg$smoke==1,1,0),
 #   Obese_Anemia_Hypertension= ifelse(merg$ex_anemia_ind==1& merg$obese==1 & merg$ex_htn_narrow_ind==1,1,0),
 #   Anemia_sev_Thinness_Diabetes= ifelse(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$ex_diab_narrow_ind==1,1,0),
  #  Anemia_sev_Thinness_Smoking= ifelse(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$smoke==1,1,0),
  #  Anemia_sev_Thinness_Hypertension= ifelse(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$ex_htn_narrow_ind==1,1,0),
  #  Hypertension_sev_Thinness_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1 & merg$ex_anemia_ind==1,1,0),
  #  Hypertension_Diabetes_sev_Thinness= ifelse(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1 & merg$sev_underweight==1,1,0))

###make systematic interaction dummies, 3 interactions

merg <- merg %>% 
  mutate( 
       obese_diabetes_hypertension= ifelse(merg$obese==1& merg$ex_diab_narrow_ind==1 & merg$ex_htn_narrow_ind==1,1,0),
   obese_diabetes_smoke= ifelse(merg$obese==1& merg$ex_diab_narrow_ind==1 & merg$smoke==1,1,0),
   obese_diabetes_smokeless= ifelse(merg$obese==1& merg$ex_diab_narrow_ind==1 & merg$csmkls_tb==1,1,0),
   obese_hypertension_smoke= ifelse(merg$obese==1& merg$ex_htn_narrow_ind==1 & merg$smoke==1,1,0),
   obese_hypertension_smokeless= ifelse(merg$obese==1& merg$ex_htn_narrow_ind==1 & merg$csmkls_tb==1,1,0),
   obese_smoke_smokeless= ifelse(merg$obese==1& merg$smoke==1 & merg$csmkls_tb==1,1,0),
   underweight_diabetes_hypertension= ifelse(merg$sev_underweight==1& merg$ex_diab_narrow_ind==1 & merg$ex_htn_narrow_ind==1,1,0),
   underweight_diabetes_smoke= ifelse(merg$sev_underweight==1& merg$ex_diab_narrow_ind==1 & merg$smoke==1,1,0),
   underweight_diabetes_smokeless= ifelse(merg$sev_underweight==1& merg$ex_diab_narrow_ind==1 & merg$csmkls_tb==1,1,0),
   underweight_hypertension_smoke= ifelse(merg$sev_underweight==1& merg$ex_htn_narrow_ind==1 & merg$smoke==1,1,0),
   underweight_hypertension_smokeless= ifelse(merg$sev_underweight==1& merg$ex_htn_narrow_ind==1 & merg$csmkls_tb==1,1,0),
   underweight_smoke_smokeless= ifelse(merg$sev_underweight==1& merg$smoke==1 & merg$csmkls_tb==1,1,0),
   diabetes_hypertension_smoke= ifelse(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1 & merg$smoke==1,1,0),
   diabetes_hypertension_smokeless= ifelse(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1 & merg$csmkls_tb==1,1,0),
   diabetes_smoke_smokeless= ifelse(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$csmkls_tb==1,1,0), 
   hypertension_smoke_smokeless= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$csmkls_tb==1,1,0))

 
########interaction dummies RIGHT randomized
merg <- merg %>% 
  mutate( 
diabetes_hypertension= ifelse(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1,1,0),
diabetes_smoke= ifelse(merg$ex_diab_narrow_ind==1& merg$smoke==1,1,0),
diabetes_smokeless= ifelse(merg$ex_diab_narrow_ind==1& merg$csmkls_tb==1,1,0),
diabetes_obese= ifelse(merg$ex_diab_narrow_ind==1& merg$obese==1,1,0),
diabetes_underweight= ifelse(merg$ex_diab_narrow_ind==1& merg$sev_underweight==1,1,0),
hypertension_smoke= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1,1,0),
hypertension_smokeless= ifelse(merg$ex_htn_narrow_ind==1& merg$csmkls_tb==1,1,0),
hypertension_obese= ifelse(merg$ex_htn_narrow_ind==1& merg$obese==1,1,0),
hypertension_underweight= ifelse(merg$ex_htn_narrow_ind==1& merg$sev_underweight==1,1,0),
smoke_smokeless= ifelse(merg$smoke==1& merg$csmkls_tb==1,1,0),
smoke_obese = ifelse(merg$smoke==1& merg$obese==1,1,0),
smoke_underweight= ifelse(merg$smoke==1& merg$sev_underweight==1,1,0),
smokeless_obese= ifelse(merg$csmkls_tb==1& merg$obese==1,1,0),
smokeless_underweight= ifelse(merg$csmkls_tb==1& merg$sev_underweight==1,1,0),
obese_underweight= ifelse(merg$obese==1& merg$sev_underweight==1,1,0))





###

#DHS <- filter(merg_noNAinrisc, svy=="DHS")
#DLHS <- filter(merg_noNAinrisc, svy=="DLHS")
#AHS <- filter(merg_noNAinrisc, svy=="AHS")


length(which(merg$svy=="DHS" & merg$smoke==1 ))
length(which(merg$svy=="DLHS" & merg$smoke==1))
length(which(merg$svy=="AHS" & merg$smoke==1))

length(which(merg$svy=="DHS" & merg$csmkls_tb==1 ))
length(which(merg$svy=="DLHS" & merg$csmkls_tb==1))
length(which(merg$svy=="AHS" & merg$csmkls_tb==1))

length(which(merg$svy=="DHS" & merg$obese==1 ))
length(which(merg$svy=="DLHS" & merg$obese==1))
length(which(merg$svy=="AHS" & merg$obese==1))

length(which(merg$svy=="DHS" & merg$sev_underweight==1 ))
length(which(merg$svy=="DLHS" & merg$sev_underweight==1))
length(which(merg$svy=="AHS" & merg$sev_underweight==1))

length(which(merg$svy=="DHS" & merg$ex_diab_narrow_ind==1 ))
length(which(merg$svy=="DLHS" & merg$ex_diab_narrow_ind==1))
length(which(merg$svy=="AHS" & merg$ex_diab_narrow_ind==1))

length(which(merg$svy=="DHS" & merg$ex_htn_narrow_ind==1 ))
length(which(merg$svy=="DLHS" & merg$ex_htn_narrow_ind==1))
length(which(merg$svy=="AHS" & merg$ex_htn_narrow_ind==1))


length(which(merg$svy=="DHS" & merg$smoke==1 &merg$sex==1))
length(which(merg$svy=="DLHS" & merg$smoke==1&merg$sex==1))
length(which(merg$svy=="AHS" & merg$smoke==1&merg$sex==1))

length(which(merg$svy=="DHS" & merg$smoke==1 &merg$sex==0))
length(which(merg$svy=="DLHS" & merg$smoke==1&merg$sex==0))
length(which(merg$svy=="AHS" & merg$smoke==1&merg$sex==0))

length(which(merg$svy=="DHS" &merg$sex==0))
length(which(merg$svy=="DLHS"&merg$sex==0))
length(which(merg$svy=="AHS"&merg$sex==0))


length(which(merg$svy=="DHS" &merg$sex==1))
length(which(merg$svy=="DLHS"&merg$sex==1))
length(which(merg$svy=="AHS"&merg$sex==1))


    

#######check if interactions are in all datasets

length(which(merg$svy=="DHS" & merg$Diabetes_Hypertension==1))
length(which(merg$svy=="DHS" & merg$sev_Thinness_Diabetes==1))
length(which(merg$svy=="DHS" & merg$Obese_Diabetes==1))
length(which(merg$svy=="DHS" & merg$Smoking_Diabetes==1))
length(which(merg$svy=="DHS" & merg$sev_Thinness_Diabetes==1))
length(which(merg$svy=="DHS" & merg$Hypertension_Anemia==1))
length(which(merg$svy=="DHS" & merg$Obese_Hypertension==1))
length(which(merg$svy=="DHS" & merg$sev_Thinness_Hypertension==1))
length(which(merg$svy=="DHS" & merg$Smoking_Hypertension==1))
length(which(merg$svy=="DHS" & merg$Obese_Anemia==1))
length(which(merg$svy=="DHS" & merg$Obese_Smoking==1))
length(which(merg$svy=="DHS" & merg$Smoking_sev_Thinness==1))
length(which(merg$svy=="DHS" & merg$Anemia_sev_Thinness==1))
length(which(merg$svy=="DHS" & merg$Anemia_Smoking==1))


length(which(merg$svy=="DLHS" & merg$Diabetes_Hypertension==1))
length(which(merg$svy=="DLHS" & merg$sev_Thinness_Diabetes==1))
length(which(merg$svy=="DLHS" & merg$Obese_Diabetes==1))
length(which(merg$svy=="DLHS" & merg$Smoking_Diabetes==1))
length(which(merg$svy=="DLHS" & merg$sev_Thinness_Diabetes==1))
length(which(merg$svy=="DLHS" & merg$Hypertension_Anemia==1))
length(which(merg$svy=="DLHS" & merg$Obese_Hypertension==1))
length(which(merg$svy=="DLHS" & merg$sev_Thinness_Hypertension==1))
length(which(merg$svy=="DLHS" & merg$Smoking_Hypertension==1))
length(which(merg$svy=="DLHS" & merg$Obese_Anemia==1))
length(which(merg$svy=="DLHS" & merg$Obese_Smoking==1))
length(which(merg$svy=="DLHS" & merg$Smoking_sev_Thinness==1))
length(which(merg$svy=="DLHS" & merg$Anemia_sev_Thinness==1))
length(which(merg$svy=="DLHS" & merg$Anemia_Smoking==1))


length(which(merg$svy=="AHS" & merg$Diabetes_Hypertension==1))
length(which(merg$svy=="AHS" & merg$sev_Thinness_Diabetes==1))
length(which(merg$svy=="AHS" & merg$Obese_Diabetes==1))
length(which(merg$svy=="AHS" & merg$Smoking_Diabetes==1))
length(which(merg$svy=="AHS" & merg$sev_Thinness_Diabetes==1))
length(which(merg$svy=="AHS" & merg$Hypertension_Anemia==1))
length(which(merg$svy=="AHS" & merg$Obese_Hypertension==1))
length(which(merg$svy=="AHS" & merg$sev_Thinness_Hypertension==1))
length(which(merg$svy=="AHS" & merg$Smoking_Hypertension==1))
length(which(merg$svy=="AHS" & merg$Obese_Anemia==1))
length(which(merg$svy=="AHS" & merg$Obese_Smoking==1))
length(which(merg$svy=="AHS" & merg$Smoking_sev_Thinness==1))
length(which(merg$svy=="AHS" & merg$Anemia_sev_Thinness==1))
length(which(merg$svy=="AHS" & merg$Anemia_Smoking==1))

# create stratum ID #stratum exists but AHS AND DLHS only have missings, so I create a new stratum ID
merg$urban_str <- as.character(merg$urban)
merg <- mutate(merg, 
                    state_dist_str = str_c(ex_state_ind, ex_d_name_ind , sep = "_"))
merg <- mutate(merg, 
                    stratumid = str_c(state_dist_str, urban_str, sep = "_")) 
merg$stratumid <- as.factor(merg$stratumid)

#create PSU ID:
merg <- merg %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
merg$psuid <- as.factor(merg$psuid)


merg_women <- filter(merg, sex==1)
merg_men <- filter(merg, sex==0)

DHS <- filter(merg, svy=="DHS")
DLHS <- filter(merg, svy=="DLHS")
AHS <- filter(merg, svy=="AHS")


#merge age standardization weight from GBD India pop into the dataset
merg <-merg %>%
  mutate(age_5yr_2=ifelse(age>=65& age<=69,11,
                                         ifelse(age>=70&age<=74,12,
                                                ifelse(age>=75 &age<=79,13,
                                                      ifelse( age>=80,14,age_5yr)))))

GBDpopweights_2018.04.24.age_grp15.19 <- read.csv("~/iCloud Drive (Archive)/Documents/Public Health Files/Public Health/Multiple Morbidities/Datasets/GBDpopweights_2018-04-24-age_grp15-19.csv")

agest_india <- GBDpopweights_2018.04.24.age_grp15.19
agest_india$sex <-as.factor(agest_india$sex)
merg$sex <- as.factor(merg$sex)

merg <- left_join(merg, agest_india) 
merg <- mutate(merg, 
                    sworld_weight_india = sweight_merge*gbd_weight)

merg <- mutate(merg, 
                    sworld_weight_india = ifelse(is.na(gbd_weight)==TRUE, mean(sworld_weight_india, na.rm=TRUE), sworld_weight_india))


####check what is in which age group

#aggregate(merg$age, by=list(merg$age), FUN=mean)
#aggregate(merg$sev_underweight_dbl, by=list(merg$age), FUN=sum)
#aggregate(merg$obese_dbl, by=list(merg$age), FUN=sum)
#aggregate(merg$ex_diab_narrow_ind_dbl, by=list(merg$age), FUN=sum)
#aggregate(merg$ex_htn_narrow_ind_dbl, by=list(merg$age), FUN=sum)
#aggregate(merg$csmkls_tb_dbl, by=list(merg$age), FUN=sum)

#aggregate(DHS$smoke_dbl, by=list(DHS$age), FUN=sum)
#aggregate(DHS$sev_underweight_dbl, by=list(DHS$age), FUN=sum)
#aggregate(DHS$obese_dbl, by=list(DHS$age), FUN=sum)
#aggregate(DHS$ex_diab_narrow_ind_dbl, by=list(DHS$age), FUN=sum)
#aggregate(DHS$ex_htn_narrow_ind_dbl, by=list(DHS$age), FUN=sum)
#aggregate(DHS$csmkls_tb_dbl, by=list(DHS$age), FUN=sum)

#aggregate(AHS$smoke_dbl, by=list(AHS$age), FUN=sum)
#aggregate(AHS$sev_underweight_dbl, by=list(AHS$age), FUN=sum)
#aggregate(AHS$obese_dbl, by=list(AHS$age), FUN=sum)
#aggregate(AHS$ex_diab_narrow_ind_dbl, by=list(AHS$age), FUN=sum)
#aggregate(AHS$ex_htn_narrow_ind_dbl, by=list(AHS$age), FUN=sum)
#aggregate(AHS$csmkls_tb_dbl, by=list(AHS$age), FUN=sum)

#aggregate(DLHS$smoke_dbl, by=list(DLHS$age), FUN=sum)
#aggregate(DLHS$sev_underweight_dbl, by=list(DLHS$age), FUN=sum)
#aggregate(DLHS$obese_dbl, by=list(DLHS$age), FUN=sum)
#aggregate(DLHS$ex_diab_narrow_ind_dbl, by=list(DLHS$age), FUN=sum)
#aggregate(DLHS$ex_htn_narrow_ind_dbl, by=list(DLHS$age), FUN=sum)


summary(AHS$obese)
summary(AHS$sev_underweight)
summary(AHS$ex_diab_narrow_ind)
summary(AHS$ex_htn_narrow_ind)
summary(AHS$smoke)
summary(AHS$csmkls_tb_dbl)

summary(DLHS$obese)
summary(DLHS$sev_underweight)
summary(DLHS$ex_diab_narrow_ind)
summary(DLHS$ex_htn_narrow_ind)
summary(DLHS$smoke)
summary(DLHS$csmkls_tb_dbl)

summary(DHS$obese)
summary(DHS$sev_underweight)
summary(DHS$ex_diab_narrow_ind)
summary(DHS$ex_htn_narrow_ind)
summary(DHS$smoke)
summary(DHS$csmkls_tb_dbl)


###DLHS 18-120
###AHS 18-100
###merg 15-54

merg <- mutate( merg,
                      married = ifelse( merg$marital==2, 1, 0))

merg$married <- as.factor(merg$married)


merg <- mutate( merg,
                      marriednames = ifelse( merg$marital==2, "Married","Not married"))

merg$marriednames <- as.factor(merg$marriednames)

merg <- within(merg, marriednames<- relevel(marriednames, ref = "Not married"))


#######exclude DHS from dataset

merg <- filter(merg, svy== "DLHS" | svy=="AHS")

###original dataset without DHS

DHLSandAHS <- filter(`DLHS4&AHS&DHS.merged(15.05.2018)`, svy== "DLHS" | svy=="AHS")




####make new regions

merg <- mutate(merg, 
                  # Nothern
                  zone_new = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir" | ex_state_ind=="Uttarakhand", "North",
                                          # Northeastern
                                          ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                 # Central
                                                 ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh"  | ex_state_ind== "Uttar Pradesh", "Central",
                                                        # Eastern
                                                        ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                               # Western
                                                               ifelse(ex_state_ind=="Daman and Diu"  | ex_state_ind=="Dadra and Nagar Haveli"| ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                      # Southern
                                                                      ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala"  | ex_state_ind=="Lakshadweep" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))







```










```{r Analyse DHS Input }


# Data cleaning and merging
library(tidyverse) 
library(dplyr) 
library(forcats) # for categorical variables (R for data science rec) --> see https://rdrr.io/cran/forcats/man/fct_unify.html
library(stringr) # for manipulating string variables (R for data science rec)
#library(lubridate) # for dates and times (R for data science rec)
#instalibrary(dummies) # to easily create dummies
library(ggplot2) 
library(ggrepel) # to avoid text labels in ggplot from overlapping
library(modelr) # to use "add_predictions()" for adding a column of predicted vals to your dataset
library(broom) # to create tidy data from model output
#library(margins) # R equivalent of Stata's margins command --> Thomas Leeper said this only to be used for marginal effects (not prediction)
#library(prediction) # Thomas Leeper's R package to get predicted probabilities
library(srvyr)  # survey package that also works with dplyr 
library(lmtest) # for likelihood ratio tests
library(sandwich) # for robust standard errors 
#library(multiwayvcov) # for clustered standard errors
library(miceadds) # package to cluster SEs more easily than in multiwayvcov; it uses multiwayvcov, so the results between the two packages are exactly the same. 
library(speedglm)
library(foreign) # for importing non-csv datasets
library(readstata13) # foreign only reads Stata 12 or lower
#library(lme4) # for multi-level modeling
#library(lmerTest) # for p-values with the lmer command
#library(sjPlot) # for plotting lmer models
#library(texreg) # for tables
library(tableone) # Creates a table 1 (summary characteristics)
#library(mice) # md.pattern() function to see patterns of missing data 
#library(reshape) # to use the rescalar function

#library(car) # for easy attaching of new variables
#library(arm)
#library(mosaic)
#library(mosaicData)
library(mediation)  # for mediation analysis
#library(lattice)
#library(pander)
library(haven)
#install.packages("eulerr")
library(eulerr)
library(UpSetR)
library(RColorBrewer)
library(lme4)




######Analyse DHS DATASET

#DHS.India.updated <- read.csv("~/Documents/Public Health Files/Public Health/public health/DHS with smoking and smokeless.csv")
#dhs <- DHS.India.updated
#dhs <- as_tibble(dhs)

DHS.with.HIV.smoke.smokeless.asthma.heart.thyroid.cancer.correct <- read.csv("~/Documents/Public Health Files/Public Health/public health/Datasets/DHS with HIV smoke smokeless asthma heart thyroid cancer correct.csv")
dhs <- DHS.with.HIV.smoke.smokeless.asthma.heart.thyroid.cancer.correct
dhs <- as_tibble(dhs)

setwd("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity")

#dhs <- filter(dhs, is.na(dhs$hiv01)==F)


dhs <- mutate(dhs,
                hypt = ifelse(hypt==1 & bp_ms==0, NA, hypt))
dhs <- mutate(dhs,
              hypt_med = ifelse(hypt_med==1 & bp_ms==0, NA, hypt_med))

####Hypertension new definition only based on two last measurements


dhs <- mutate(dhs,
             sbp_avg_new = ifelse(is.na(sbp2)==F & is.na(sbp3)==F,(sbp2+sbp3)/2,
                              ifelse(is.na(sbp2)==T & is.na(sbp3)==F,sbp3,
                                     ifelse(is.na(sbp2)==F & is.na(sbp3)==T,sbp2,NA))))

dhs <- mutate(dhs,
             dbp_avg_new = ifelse(is.na(dbp2)==F & is.na(dbp3)==F,(dbp2+dbp3)/2,
                              ifelse(is.na(dbp2)==T & is.na(dbp3)==F,dbp3,
                                     ifelse(is.na(dbp2)==F & is.na(dbp3)==T,dbp2,NA))))

dhs <- mutate(dhs,
             ex_htn_narrow_ind = ifelse(sbp_avg_new>=140 | dbp_avg_new>=90,1,0))


dhs <-mutate(dhs,
             ex_htn_broad_ind = ifelse(is.na(ex_htn_narrow_ind)==T | is.na(hypt) ==T | is.na(hypt_med)==T, NA,ifelse(hypt_med==1 | hypt==1 | ex_htn_narrow_ind==1, 1, 0)))
 

dhs <- mutate(dhs, p_wt_new = ifelse(sex==0 & age==15, p_wt* { ( (25101) / (12159708) )   /   ( (3761) / (13739746) ) },
                                     ifelse(sex==0 & age==16,  p_wt* { ( (24702) / (11564358) )   /   ( (3844) / (13027935) ) },
                                            ifelse(sex==0 & age==17,  p_wt* { ( (22674) / (9868018) )   /   ( (3725) / (11349449) ) },
                                                   ifelse(sex==0 & age==18,   p_wt* { ( (25320) / (12937296) )   /   ( (4177) / (15020851) ) },
                                                          ifelse(sex==0 & age==19,   p_wt* { ( (19462) / (10014673) )   /   ( (3203) / (10844415) ) },
                                                                 ifelse(sex==0 & age==20,   p_wt* { ( (23573) / (13990570) )   /   ( (3632) / (14892165) ) },
                                                                        ifelse(sex==0 & age==21,  p_wt* { ( (18442) / (9446694) )   /   ( (3028) / (10532278) ) },
                                                                               ifelse(sex==0 & age==22,   p_wt* { ( (22212) / (11135249) )   /   ( (3472) / (12392976) ) },
                                                                                      ifelse(sex==0 & age==23,   p_wt* { ( (19660) / (9479866) )   /   ( (2989) / (9674189) ) },
                                                                                             ifelse(sex==0 & age==24,   p_wt* { ( (19262) / (9787150) )   /   ( (3061) / (10093085) ) },
                                                                                                    ifelse(sex==0 & age==25,  p_wt* { ( (25318) / (13456554) )   /   ( (3821) / (14311524) ) },
                                                                                                           ifelse(sex==0 & age==26,   p_wt* { ( (19390) / (9761967) )   /   ( (3131) / (10315030) ) },
                                                                                                                  ifelse(sex==0 & age==27,   p_wt* { ( (18113) / (8157318) )   /   ( (2957) / (8552032) ) },
                                                                                                                         ifelse(sex==0 & age==28,   p_wt* { ( (22299) / (11407090) )   /   ( (3413) / (10719926) ) },
                                                                                                                                ifelse(sex==0 & age==29,   p_wt* { ( (15413) / (7286828) )   /   ( (2476) / (7445696) ) },
                                                                                                                                       ifelse(sex==0 & age==30,  p_wt* { ( (27923) / (14770033) )   /   ( (4356) / (15628996) ) },
                                                                                                                                              ifelse(sex==0 & age==31,   p_wt* { ( 13757 / (6665743) )   /   ( (2190) / (7157502) ) },
                                                                                                                                                     ifelse(sex==0 & age==32, p_wt* { ( (20375) / (8812439) )   /   ( (3156) / (8801105) ) },
                                                                                                                                                            ifelse(sex==0 & age==33,  p_wt* { ( (14514) / (6655662) )   /   ( (2299) / (6108879) ) },
                                                                                                                                                                   ifelse(sex==0 & age==34,   p_wt* { ( (14285) / (7030400) )   /   ( (2348) / (6964192) ) },
                                                                                                                                                                          ifelse(sex==0 & age==35,   p_wt* { ( (27035) / (13385965) )   /   ( (4391) / (15036666) ) },
                                                                                                                                                                                 ifelse(sex==0 & age==36,   p_wt* { ( (15670) / (7760149) )   /   ( (2435) / (8067568) ) },
                                                                                                                                                                                        ifelse(sex==0 & age==37,   p_wt* { ( (13795) / (5907352) )   /   ( (2176) / (5784879) ) },
                                                                                                                                                                                               ifelse(sex==0 & age==38,   p_wt* { ( (18693) / (9381357) )   /   ( (2723) / (8090401) ) },
                                                                                                                                                                                                      ifelse(sex==0 & age==39,   p_wt* { ( (12683) / (5786480) )   /   ( (1968) / (5939867) ) },
                                                                                                                                                                                                             ifelse(sex==0 & age==40,   p_wt* { ( (25682) / (13355581) )   /   ( (3974) / (15173411) ) },
                                                                                                                                                                                                                    ifelse(sex==0 & age==41,  p_wt* { ( (11046) / (5395597) )   /   ( (1819) / (6172297) ) },
                                                                                                                                                                                                                           ifelse(sex==0 & age==42,  p_wt* { ( (16124) / (6523816) )   /   ( (2544) / (6856826) ) },
                                                                                                                                                                                                                                  ifelse(sex==0 & age==43,   p_wt* { ( (11983) / (4865438) )   /   ( (1797) / (4468914) ) },
                                                                                                                                                                                                                                         ifelse(sex==0 & age==44,   p_wt* { ( (10836) / (4752294) )   /   ( (1714) / (4873938) ) },
                                                                                                                                                                                                                                                ifelse(sex==0 & age==45,  p_wt* { ( (23877) / (11187786) )   /   ( (3854) / (12685175) ) },
                                                                                                                                                                                                                                                       ifelse(sex==0 & age==46,  p_wt* { ( (11752) / (5257138) )   /   ( (1895) / (5735540) ) },
                                                                                                                                                                                                                                                              ifelse(sex==0 & age==47,   p_wt* { ( (11295) / (3908175) )   /   ( (1714) / (4043122) ) },
                                                                                                                                                                                                                                                                     ifelse(sex==0 & age==48, p_wt* { ( (14786) / (6081038) )   /   ( (2119) / (5568554) ) },
                                                                                                                                                                                                                                                                            ifelse(sex==0 & age==49,   p_wt* { ( (10399) / (3746076) )   /   ( (1506) / (4105723)) }, p_wt ))))))))))))))))))))))))))))))))))))
dhs <- mutate(dhs, p_wt_new = ifelse(sex==0 & age>=50, p_wt_new/1.013194, p_wt_new )) 
dhs <- mutate(dhs, p_wt_new = ifelse(sex==0 & age>=50, p_wt * { ((6.72342) * (1.013194)) / (0.998047) }, p_wt_new ))   







# 3. Filter out those <18 or pregnant  #
####dhs_nomiss <- dplyr::filter(dhs_nomiss, age> 18) # only those >18 and with non-missing age
dhs <- dplyr::filter(dhs, pregnant == 0)# only keep those who aren't pregnant (they didn't measure glucose in pregnant women anyway); dhs_nomiss has no missings in the pregnant variable

dhs <- dplyr::filter(dhs, age < 50)

##Define diabetics

dhs <- mutate(dhs, ex_glucose_ind=
                            ifelse(dhs$ex_glucose_ind>900 | is.na(ex_glucose_ind)==T,NA,ex_glucose_ind))
dhs <- mutate(dhs, fbg=
                ifelse(dhs$fbg>49.95 | is.na(fbg)==T,NA,fbg))
dhs <- mutate(dhs, ex_glucose_ind = ex_glucose_ind*1.11)


dhs <- mutate(dhs,
              ex_diab_narrow_ind = ifelse( is.na(ex_glucose_ind)==T, NA, 
                                           ifelse(ex_glucose_ind>=200, 1, 
                                                  ifelse(fast==1 & ex_glucose_ind>=126, 1, 0))))


dhs$ex_diab_narrow_ind <- as.factor(dhs$ex_diab_narrow_ind)

dhs <-mutate(dhs,
             ex_diab_broad_ind =  ifelse(is.na(ex_diab_narrow_ind) ==T | is.na(hbg12)==T, NA,  # wir definieren unser sample als diejenige, fuer die wir die Variablen haben, um bestimmen zu koennen ob sie diab broad haben
                                         ifelse(hbg12==1 | ex_diab_narrow_ind==1, 1, 0)))
dhs$ex_diab_broad_ind <- as.factor(dhs$ex_diab_broad_ind)





#### anemia definition

dhs <- dplyr::mutate(dhs,
                           mild_anemia = ifelse((sex = 1 & ex_hb_adj_ind < 12 & ex_hb_adj_ind >= 11) | (sex = 0 & ex_hb_adj_ind < 13 &ex_hb_adj_ind >= 11 ), 1, 0))
dhs$mild_anemia <- factor(dhs$mild_anemia, levels = c("0", "1"))

dhs <- dplyr::mutate(dhs,
                            moderate_anemia = ifelse((sex = 1 & ex_hb_adj_ind < 11 & ex_hb_adj_ind >= 8) | (sex = 0 & ex_hb_adj_ind < 11 & ex_hb_adj_ind >= 8 ), 1, 0))
dhs$moderate_anemia <- factor(dhs$moderate_anemia, levels = c("0", "1"))

dhs <- dplyr::mutate(dhs,
                            severe_anemia = ifelse((sex = 1 & ex_hb_adj_ind < 8) | (sex = 0 & ex_hb_adj_ind < 8 ), 1, 0))
dhs$severe_anemia <- factor(dhs$severe_anemia, levels = c("0", "1")) 


dhs <- dplyr::mutate(dhs,
                           anemia = ifelse(moderate_anemia==1 | severe_anemia==1,1,0))
dhs$anemia <- factor(dhs$anemia, levels = c("0", "1")) 


dhs <- mutate(dhs, 
               obese = ifelse(bmi>=27.5,1,0))





###bmi group
dhs <- mutate(dhs,
                     bmi_group = ifelse(bmi<18.5, "<18.5kg/m2",
                                        ifelse(bmi>=18.5 & bmi<23, "18.5-22.9 kg/m2",
                                               ifelse(bmi>=23 & bmi<25, "23.0-24.9 kg/m2",
                                                      ifelse(bmi>=25 & bmi<27.5, "25.0-27.4 kg/m2",
                                                             ifelse(bmi>=27.5 & bmi<30, "27.5-29.9 kg/m2",
                                                                    ifelse(bmi>=30, ">= 30.0 kg/m2", NA)))))))


dhs$bmi_group <- factor(dhs$bmi_group, levels = c("<18.5kg/m2", "18.5-22.9 kg/m2", "23.0-24.9 kg/m2", "25.0-27.4 kg/m2", "27.5-29.9 kg/m2", ">= 30.0 kg/m2"))
dhs <- within(dhs, bmi_group<- relevel(bmi_group, ref = "18.5-22.9 kg/m2"))


dhs <- mutate(dhs, 
               obese = ifelse(bmi>=27.5,1,0))


dhs_nomiss <- filter(dhs, is.na(ex_diab_broad_ind)==F & is.na(ex_htn_broad_ind)==F & is.na(anemia)==F & is.na(asthma)==F & is.na(obese)==F)


dhs_nomiss <- mutate(dhs_nomiss, 
               obese = ifelse(bmi>=27.5,1,0))

#works missing weights as average

dhs_nomiss <- mutate(dhs_nomiss, 
              p_wt_new = ifelse(is.na(p_wt_new)==TRUE, mean(p_wt_new, na.rm=TRUE), p_wt_new))

dhs_nomiss <- dplyr::mutate(dhs_nomiss, 
                     p_wt_newfemale = ifelse(sex==1, dhs_nomiss$p_wt_new, NA))

dhs_nomiss <- dplyr::mutate(dhs_nomiss, 
                     p_wt_newmale = ifelse(sex==0, dhs_nomiss$p_wt_new, NA))






# 9. Create age group women and men, educat group in dhs #
dhs <- dplyr::mutate(dhs, age_grp = ifelse(age<=24, "15-24", 
                                                 ifelse(age>24 & age<=34, "25-34",
                                                        ifelse(age>34 & age<=44, "35-44",
                                                               ifelse(age>44, "45-54"))))) 
dhs$age_grp <- factor(dhs$age_grp, levels = c("15-24", "25-34", "35-44", "45-54"))
dhs <- within(dhs, age_grp <- relevel(age_grp, ref = "15-24"))


#age group by sex


dhs <- dplyr::mutate(dhs, age_grp_women = ifelse(age<=24 & sex==1, "15-24", 
                                                               ifelse(age>24 & sex==1 & age<=34, "25-34",
                                                                      ifelse(age>34 & sex==1 & age<=44, "35-44",
                                                                             ifelse(age>44 & sex==1 & age<=49, "45-49",">49"))))) 
dhs$age_grp_women <- factor(dhs$age_grp_women, levels = c("15-24", "25-34", "35-44", "45-49", ">49"))
dhs <- within(dhs, age_grp_women <- relevel(age_grp_women, ref = "15-24"))


dhs <- dplyr::mutate(dhs, age_grp_men = ifelse(age>14 & sex==0 & age<=24, "15-24", 
                                                             ifelse(age>24 & sex==0 & age<=34, "25-34",
                                                                    ifelse(age>34 & sex==0 & age<=44, "35-44",
                                                                           ifelse(age>44 & sex==0 & age<=54, "45-54", ">54")))))
dhs$age_grp_men <- factor(dhs$age_grp_men, levels = c("15-24", "25-34", "35-44", "45-54", ">54"))
dhs <- within(dhs, age_grp_men <- relevel(age_grp_men, ref = "15-24"))

##educatgroups

dhs <- dplyr::mutate(dhs, educatnames = ifelse(educat_lcl==1, "No formal education", 
                                                             ifelse(educat_lcl==2, "< Primary school",
                                                                    ifelse(educat_lcl==3, "Primary school",
                                                                           ifelse(educat_lcl==4, "Middle school",
                                                                                  ifelse(educat_lcl==5, "Secondary school",
                                                                                          ifelse(educat_lcl==6, "> Secondary school",NA))))))) 
dhs$educatnames <- factor(dhs$educatnames, levels = c("No formal education", "< Primary school", "Primary school", "Middle school", "Secondary school", "> Secondary school", NA))
dhs <- within(dhs, educatnames<- relevel(educatnames, ref = "No formal education"))

##marriedgroups
dhs <- dplyr::mutate(dhs, maritalnames = ifelse(marital==1, "Never married", 
                                                              ifelse(marital==2, "Currently married",
                                                                     ifelse(marital==3, "Separated",
                                                                            ifelse(marital==4, "Divorced",
                                                                                   ifelse(marital==5, "Widowed",
                                                                                          ifelse(marital==6, "Cohabitating",
                                                                                                 ifelse(marital==7, "divorced or separated or deserted or widowed","Refused")))))))) 
dhs$maritalnames <- factor(dhs$maritalnames, levels = c("Never married", "Currently married", "Separated", "Divorced", "Widowed", "Cohabitating", "divorced/separated/deserted/widowed", "Refused"))
dhs <- within(dhs, maritalnames<- relevel(maritalnames, ref = "Never married"))

###create married variable in dhs


dhs <- mutate( dhs,
                      married = ifelse( dhs$marital==2, 1, 0))

dhs$married <- as.factor(dhs$married)


dhs <- mutate( dhs,
                      marriednames = ifelse( dhs$marital==2, "Married","Not married"))

dhs$marriednames <- as.factor(dhs$marriednames)

dhs <- within(dhs, marriednames<- relevel(marriednames, ref = "Not married"))



dhs <- mutate(dhs,
                     age_grpOR = 
                       as.factor(ifelse(
                         age>=15 & age<20, "15-19",
                         ifelse(age>=20 & age<25, "20-24",
                                ifelse(age>=25 & age<30, "25-29",
                                       ifelse(age>=30 & age<35,"30-34", 
                                              ifelse(age>=35 & age<40, "35-39",
                                                     ifelse(age>=40 & age<45, "40-44",
                                                            ifelse(age>=45 & age<50, "45-49",
                                                                   ifelse(age>=50 & age<55, "50-54",NA))))))))))





# Diabetes medication as factor in dhs

dhs$ex_dia_med_ind <- as.factor(dhs$ex_dia_med_ind)

######wealth_quintile_rurb as factor in dhs

dhs$wealth_quintile_rurb <- as.factor(dhs$wealth_quintile_rurb)


# create labels for female, rural, rich in dhs

dhs <- mutate(dhs, 
                     rural = ifelse(urban==1, 0, 1),
                     female = ifelse(sex==1, 1, 0),
                     male = ifelse(sex==1, 0, 1))
dhs$rural <- as.factor(dhs$rural)
dhs$female <- as.factor(dhs$female)
dhs$male <- as.factor(dhs$male)



dhs <- dhs %>% 
  mutate(female_lab = as.factor(ifelse(is.na(female) == TRUE, NA, 
                                       ifelse(female == 1, "Female", "Male"))),
         urban_lab = as.factor(ifelse(is.na(rural) == TRUE, NA, 
                                      ifelse(rural == 1, "Rural", "Urban"))),
         wealth_quintile_rurb_lab = as.factor(ifelse(is.na(wealth_quintile_rurb) == TRUE, NA, 
                                                     ifelse( wealth_quintile_rurb == 1, "Q1 (Poorest)", 
                                                             ifelse(wealth_quintile_rurb == 2, "Q2",
                                                                    ifelse(wealth_quintile_rurb == 3, "Q3",
                                                                           ifelse(wealth_quintile_rurb == 4, "Q4",
                                                                                  ifelse(wealth_quintile_rurb == 5, "Q5 (Richest)", NA))))))))

dhs$wealth_quintile_rurb_lab <- as.factor(dhs$wealth_quintile_rurb_lab)
dhs<- within(dhs, wealth_quintile_rurb_lab <- relevel(wealth_quintile_rurb_lab, ref = "Q1 (Poorest)"))




# 9. Create age group women and men, educat group in dhs_nomiss #



dhs_nomiss <- dplyr::mutate(dhs_nomiss, age_grp_old = ifelse(age<=24 , "15-24", 
                                                             ifelse(age>24 &  age<=34, "25-34",
                                                                    ifelse(age>34 &  age<=44, "35-44",
                                                                           ifelse(age>44 &  age<=54, "45-54", NA)))))


###create married variable

dhs_nomiss <- mutate( dhs_nomiss,
                      marriednames = ifelse( dhs_nomiss$marital==2, "Married","Not married"))
###Age groups

dhs_nomiss <- dplyr::mutate(dhs_nomiss, age_grp = ifelse(age<=24, "15-24", 
                                           ifelse(age>24 & age<=34, "25-34",
                                                  ifelse(age>34 & age<=44, "35-44","45-54")))) 
dhs_nomiss$age_grp <- factor(dhs_nomiss$age_grp, levels = c("15-24", "25-34", "35-44", "45-54"))
dhs_nomiss <- within(dhs_nomiss, age_grp <- relevel(age_grp, ref = "15-24"))



##age group per sex

dhs_nomiss <- dplyr::mutate(dhs_nomiss, age_grp_women = ifelse(age<=24 & sex==1, "15-24", 
                                                 ifelse(age>24 & sex==1 & age<=34, "25-34",
                                                        ifelse(age>34 & sex==1 & age<=44, "35-44", 
                                                               ifelse(age>44 &sex==1 & age<=49, "45-49", ">49"))))) 
dhs_nomiss$age_grp_women <- factor(dhs_nomiss$age_grp_women, levels = c("15-24", "25-34", "35-44", "45-49", ">49"))

dhs_nomiss <- within(dhs_nomiss, age_grp_women <- relevel(age_grp_women, ref = "15-24"))


dhs_nomiss <- dplyr::mutate(dhs_nomiss, age_grp_men = ifelse(age>14 & sex==0 & age<=24, "15-24", 
                                               ifelse(age>24 & sex==0 & age<=34, "25-34",
                                                      ifelse(age>34 & sex==0 & age<=44, "35-44",
                                                             ifelse(age>44 & sex==0 & age<=54, "45-54", ">54"))))) 
dhs_nomiss$age_grp_men <- factor(dhs_nomiss$age_grp_men, levels = c("15-24", "25-34", "35-44", "45-54", ">54"))
dhs_nomiss <- within(dhs_nomiss, age_grp_men <- relevel(age_grp_men, ref = "15-24"))

###age group for educat analysis

dhs_nomiss <- dplyr::mutate(dhs_nomiss, age_grp_educat = ifelse(age>18 & age<=26, "19-26", 
                                                         ifelse(age>26 & age<=34, "27-34",
                                                                ifelse(age>34 & age<=42, "35-42",
                                                                       ifelse(age>42 & age<=49, "43-49", NA))))) 
dhs_nomiss$age_grp_educat <- factor(dhs_nomiss$age_grp_educat, levels = c("19-26", "27-34", "35-42", "43-49"))
dhs_nomiss <- within(dhs_nomiss, age_grp_educat <- relevel(age_grp_educat, ref = "19-26"))

dhs_nomiss <- dplyr::mutate(dhs_nomiss, age_grp_educat_men =  ifelse(age>18 & age<=26, "19-26", 
                                                                     ifelse(age>26 & age<=34, "27-34",
                                                                            ifelse(age>34 & age<=42, "35-42",
                                                                                   ifelse(age>42 , "43-54", NA))))) 
dhs_nomiss$age_grp_educat_men <- factor(dhs_nomiss$age_grp_educat_men, levels = c("19-26", "27-34", "35-42", "43-54"))
dhs_nomiss <- within(dhs_nomiss, age_grp_educat_men <- relevel(age_grp_educat_men, ref = "19-26"))



####educat reform


dhs_nomiss <- dplyr::mutate(dhs_nomiss, educat = ifelse(educat==0 | educat==1 , 0, educat))

dhs_nomiss <- dplyr::mutate(dhs_nomiss, educatnames = ifelse(educat_lcl==1, "No formal education", 
                                                             ifelse(educat_lcl==2, "< Primary school",
                                                                    ifelse(educat_lcl==3, "Primary school",
                                                                           ifelse(educat_lcl==4, "Middle school",
                                                                                  ifelse(educat_lcl==5, "Secondary school",
                                                                                          ifelse(educat_lcl==6, "> Secondary school",NA))))))) 
dhs_nomiss$educatnames <- factor(dhs_nomiss$educatnames, levels = c("No formal education", "< Primary school", "Primary school", "Middle school", "Secondary school", "> Secondary school", NA))
dhs_nomiss <- within(dhs_nomiss, educatnames<- relevel(educatnames, ref = "No formal education"))



###married subgroups

dhs_nomiss <- dplyr::mutate(dhs_nomiss, maritalnames = ifelse(marital==1, "Never married", 
                                                ifelse(marital==2, "Currently married",
                                                       ifelse(marital==3, "Separated",
                                                              ifelse(marital==4, "Divorced",
                                                                     ifelse(marital==5, "Widowed",
                                                                            ifelse(marital==6, "Cohabitating",
                                                                                   ifelse(marital==7, "divorced or separated or deserted or widowed","Refused")))))))) 


dhs_nomiss$maritalnames <- factor(dhs_nomiss$maritalnames, levels = c("Never married", "Currently married", "Separated", "Divorced", "Widowed", "Cohabitating", "divorced/separated/deserted/widowed", "Refused"))
dhs_nomiss <- within(dhs_nomiss, maritalnames<- relevel(maritalnames, ref = "Never married"))




###create married variable

dhs_nomiss <- mutate( dhs_nomiss,
                      married = ifelse( dhs_nomiss$marital==2, 1, 0))

dhs_nomiss$married <- as.factor(dhs_nomiss$married)


dhs_nomiss <- mutate( dhs_nomiss,
                      marriednames = ifelse( dhs_nomiss$married==1, "Married","Not married"))

dhs_nomiss$marriednames <- as.factor(dhs_nomiss$marriednames)

dhs_nomiss <- within(dhs_nomiss, marriednames<- relevel(marriednames, ref = "Not married"))



dhs_nomiss <- mutate(dhs_nomiss,
                                   age_grpOR = 
                                     as.factor(ifelse(
                                       age>=15 & age<20, "15-19",
                                       ifelse(age>=20 & age<25, "20-24",
                                              ifelse(age>=25 & age<30, "25-29",
                                                     ifelse(age>=30 & age<35,"30-34", 
                                                            ifelse(age>=35 & age<40, "35-39",
                                                                   ifelse(age>=40 & age<45, "40-44",
                                                                          ifelse(age>=45 & age<50, "45-49",
                                                                                 ifelse(age>=50 & age<55, "50-54",NA))))))))))

dhs_nomiss$age_grpOR <- factor(dhs_nomiss$age_grpOR, levels = c("15-19","20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54"))

dhs_nomiss <- within(dhs_nomiss, age_grpOR <- relevel(age_grpOR, ref = "15-19"))



### test if unique and number
#unique(dhs$d_id)
#length(unique(dhs$d_id))
#length(unique(dhs$psu))


# Diabetes medication as factor

dhs_nomiss$ex_dia_med_ind <- as.factor(dhs_nomiss$ex_dia_med_ind)

######wealth_quintile_rurb as factor

dhs_nomiss$wealth_quintile_rurb <- as.factor(dhs_nomiss$wealth_quintile_rurb)


# create labels for female, rural, rich

dhs_nomiss <- mutate(dhs_nomiss, 
              rural = ifelse(urban==1, 0, 1),
              female = ifelse(sex==1, 1, 0),
              male = ifelse(sex==1, 0, 1))
dhs_nomiss$rural <- as.factor(dhs_nomiss$rural)
dhs_nomiss$female <- as.factor(dhs_nomiss$female)
dhs_nomiss$male <- as.factor(dhs_nomiss$male)



dhs_nomiss <- dhs_nomiss %>% 
  mutate(female_lab = as.factor(ifelse(is.na(female) == TRUE, NA, 
                                       ifelse(female == 1, "Female", "Male"))),
         urban_lab = as.factor(ifelse(is.na(rural) == TRUE, NA, 
                                      ifelse(rural == 1, "Rural", "Urban"))),
         wealth_quintile_rurb_lab = as.factor(ifelse(is.na(wealth_quintile_rurb) == TRUE, NA, 
                                                     ifelse( wealth_quintile_rurb == 1, "Q1 (Poorest)", 
                                                             ifelse(wealth_quintile_rurb == 2, "Q2",
                                                             ifelse(wealth_quintile_rurb == 3, "Q3",
                                                                    ifelse(wealth_quintile_rurb == 4, "Q4",
                                                             ifelse(wealth_quintile_rurb == 5, "Q5 (Richest)", NA))))))))

dhs_nomiss$wealth_quintile_rurb_lab <- as.factor(dhs_nomiss$wealth_quintile_rurb_lab)
dhs_nomiss<- within(dhs_nomiss, wealth_quintile_rurb_lab <- relevel(wealth_quintile_rurb_lab, ref = "Q1 (Poorest)"))

############make interaction dummies


dhs_nomiss <- mutate( dhs_nomiss,
    anemia_diabetes= ifelse(dhs_nomiss$anemia==1& dhs_nomiss$ex_diab_broad_ind==1,1,0),
  anemia_asthma= ifelse(dhs_nomiss$anemia==1& dhs_nomiss$asthma==1,1,0), 
  anemia_hypertension= ifelse(dhs_nomiss$anemia==1& dhs_nomiss$ex_htn_broad_ind==1,1,0), 
  diabetes_asthma= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$asthma==1,1,0), 
  diabetes_hypertension= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1,1,0), 
  asthma_hypertension= ifelse(dhs_nomiss$asthma==1& dhs_nomiss$ex_htn_broad_ind==1,1,0), 
  anemia_diabetes_asthma= ifelse(dhs_nomiss$anemia==1& dhs_nomiss$ex_diab_broad_ind==1 & dhs_nomiss$asthma==1,1,0), 
  anemia_diabetes_hypertension= ifelse(dhs_nomiss$anemia==1& dhs_nomiss$ex_diab_broad_ind==1 & dhs_nomiss$ex_htn_broad_ind==1,1,0), 
  anemia_asthma_hypertension= ifelse(dhs_nomiss$anemia==1& dhs_nomiss$asthma==1 & dhs_nomiss$ex_htn_broad_ind==1,1,0), 
  diabetes_asthma_hypertension= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$asthma==1 & dhs_nomiss$ex_htn_broad_ind==1,1,0),
 diabetes_asthma_hypertension_anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$asthma==1 & dhs_nomiss$ex_htn_broad_ind==1 & dhs_nomiss$anemia==1,1,0))

#merge age standardization weight from GBD India pop into the dataset
dhs_nomiss <-dhs_nomiss %>%
  mutate(age_5yr_2=ifelse(age>=65& age<=69,11,
                                         ifelse(age>=70&age<=74,12,
                                                ifelse(age>=75 &age<=79,13,
                                                      ifelse( age>=80,14,age_5yr)))))

GBDpopweights_2018.04.24.age_grp15.19 <- read.csv("~/Documents/Public Health Files/Public Health/public health/Datasets/GBDpopweights_2018-04-24-age_grp15-19.csv")

agest_india <- GBDpopweights_2018.04.24.age_grp15.19
agest_india$sex <-as.factor(agest_india$sex)
dhs_nomiss$sex <- as.factor(dhs_nomiss$sex)

dhs_nomiss <- left_join(dhs_nomiss, agest_india) 
dhs_nomiss <- mutate(dhs_nomiss, 
                    sworld_weight_india = p_wt_new*gbd_weight)

dhs_nomiss <- mutate(dhs_nomiss, 
                    sworld_weight_india = ifelse(is.na(gbd_weight)==TRUE, mean(sworld_weight_india, na.rm=TRUE), sworld_weight_india))




########basic info morbidities

dhs_nomiss$ex_htn_broad_ind_factor <- as.factor(dhs_nomiss$ex_htn_broad_ind)
dhs_nomiss$ex_diab_broad_ind_factor <- as.factor(dhs_nomiss$ex_diab_broad_ind)
dhs_nomiss$hiv03_factor <- as.factor(dhs_nomiss$hiv03)
dhs_nomiss$heart_factor <- as.factor(dhs_nomiss$heart)
dhs_nomiss$cancer_factor <- as.factor(dhs_nomiss$cancer)
dhs_nomiss$thyroid_factor <- as.factor(dhs_nomiss$thyroid)
dhs_nomiss$anemia_factor <- as.factor(dhs_nomiss$anemia)
dhs_nomiss$asthma_factor <- as.factor(dhs_nomiss$asthma)

summary(dhs_nomiss$ex_htn_broad_ind_factor)
summary(dhs_nomiss$ex_diab_broad_ind_factor)
summary(dhs_nomiss$hiv03_factor)
summary(dhs_nomiss$heart_factor)
summary(dhs_nomiss$cancer_factor)
summary(dhs_nomiss$thyroid_factor)
summary(dhs_nomiss$anemia_factor)
summary(dhs_nomiss$asthma_factor)


#######make numeric morbidities where NA is 0

dhs_nomiss <- mutate(dhs_nomiss, 
               hiv0and1 = ifelse(hiv03==1,1,0))



dhs_nomiss <- mutate(dhs_nomiss, 
              anemia_dbl = as.numeric(anemia)-1,
              asthma_dbl = as.numeric(asthma),
              hiv03_dbl = as.numeric(hiv0and1),
              ex_diab_broad_ind_dbl = as.numeric(ex_diab_broad_ind)-1,
              ex_htn_broad_ind_dbl = as.numeric(ex_htn_broad_ind),
              thyroid_dbl = as.numeric(thyroid),
              heart_dbl = as.numeric(heart),
              cancer_dbl = as.numeric(cancer))

summary(dhs_nomiss$ex_htn_broad_ind_dbl)
summary(dhs_nomiss$ex_diab_broad_ind_dbl)
summary(dhs_nomiss$hiv03_dbl)
summary(dhs_nomiss$heart_dbl)
summary(dhs_nomiss$cancer_dbl)
summary(dhs_nomiss$thyroid_dbl)
summary(dhs_nomiss$anemia_dbl)
summary(dhs_nomiss$asthma_dbl)

              


#####make NAs to 0 in dbl

dhs_nomiss[which(is.na(dhs_nomiss$ex_htn_broad_ind_dbl)==T), "ex_htn_broad_ind_dbl"]<-0

dhs_nomiss[which(is.na(dhs_nomiss$ex_diab_broad_ind_dbl)==T), "ex_diab_broad_ind_dbl"]<-0

dhs_nomiss[which(is.na(dhs_nomiss$hiv03_dbl)==T), "hiv03_dbl"]<-0
dhs_nomiss[which(is.na(dhs_nomiss$heart_dbl)==T), "heart_dbl"]<-0
dhs_nomiss[which(is.na(dhs_nomiss$cancer_dbl)==T), "cancer_dbl"]<-0
dhs_nomiss[which(is.na(dhs_nomiss$thyroid_dbl)==T), "thyroid_dbl"]<-0
dhs_nomiss[which(is.na(dhs_nomiss$anemia_dbl)==T), "anemia_dbl"]<-0
dhs_nomiss[which(is.na(dhs_nomiss$asthma_dbl)==T), "asthma_dbl"]<-0
dhs_nomiss[which(is.na(dhs_nomiss$obese)==T), "obese"]<-0

summary(dhs_nomiss$ex_htn_broad_ind_dbl)
summary(dhs_nomiss$ex_diab_broad_ind_dbl)
summary(dhs_nomiss$hiv03_dbl)
summary(dhs_nomiss$heart_dbl)
summary(dhs_nomiss$cancer_dbl)
summary(dhs_nomiss$thyroid_dbl)
summary(dhs_nomiss$anemia_dbl)
summary(dhs_nomiss$asthma_dbl)


 ######make morbidity Dummy                      

dhs_nomiss<- mutate(dhs_nomiss, 
              sum_multi = ex_diab_broad_ind_dbl + ex_htn_broad_ind_dbl  + anemia_dbl + asthma_dbl + obese)

dhs_nomiss <- mutate(dhs_nomiss, 
               sum_multi_dbl = as.numeric(sum_multi))
               
dhs_nomiss <- mutate(dhs_nomiss,
               multi_morbid = ifelse(sum_multi>=2,1,0))

dhs_nomiss <- mutate(dhs_nomiss, 
               multi_morbid_dbl = as.numeric(multi_morbid))

dhs_nomiss$multi_morbid <- as.factor(dhs_nomiss$multi_morbid)
dhs_nomiss$sum_multi <- as.factor(dhs_nomiss$sum_multi)

summary(dhs_nomiss$multi_morbid_dbl)
summary(dhs_nomiss$sum_multi_dbl)

dhs_nomiss<- mutate(dhs_nomiss, 
                    zero_morb = ifelse(sum_multi_dbl==0,1,0),
                    one_morb = ifelse(sum_multi_dbl>=1,1,0),
                     two_morb = ifelse(sum_multi_dbl>=2,1,0),
                     three_morb = ifelse(sum_multi_dbl>=3,1,0),
                     four_morb = ifelse(sum_multi_dbl>=4,1,0),
                     five_morb = ifelse(sum_multi_dbl>=5,1,0),
                     six_morb = ifelse(sum_multi_dbl>=6,1,0),
                     seven_morb = ifelse(sum_multi_dbl>=7,1,0),
                     eight_morb = ifelse(sum_multi_dbl>=8,1,0))




#######make interaction dummies

dhs_nomiss <- dhs_nomiss %>% 
  mutate( 
    Diabetes_Hypertension = ifelse(dhs_nomiss$ex_diab_narrow_ind==1& dhs_nomiss$ex_htn_narrow_ind==1,1,0),
    Diabetes_Anemia = ifelse(dhs_nomiss$ex_diab_narrow_ind==1& dhs_nomiss$anemia==1,1,0),
   Hypertension_Anemia= ifelse(dhs_nomiss$ex_htn_narrow_ind==1& dhs_nomiss$anemia==1,1,0),
    Obese_Hypertension= ifelse(dhs_nomiss$ex_htn_narrow_ind==1& dhs_nomiss$obese==1,1,0))
   



# Summary dhs_nomiss

dhs_nomiss$fast <- as.factor(dhs_nomiss$fast)



table1names <- c("ex_diab_broad_ind", "age_grpOR",
                 "age_grp_men", "age_grp_women", "fast", "educatnames", "wealth_quintile_rurb_lab", "marriednames", "urban_lab")




totalwithmiss <- CreateTableOne(vars = table1names, data=dhs_nomiss, includeNA=T)
total <- print(totalwithmiss)

#write.csv(total, file="dhs_nomiss summary.csv")

sexwithmiss <- CreateTableOne(vars = table1names, data=dhs_nomiss, strata=c("female_lab"), includeNA=T)
sexdhs_nomiss <- print(sexwithmiss)

#write.csv(sexdhs_nomiss, file= "dhs_nomiss summary per sex.csv")






# create stratum ID
dhs_nomiss$state_dist_str <- as.character(dhs_nomiss$ex_state_ind)
dhs_nomiss$rural_str <- as.character(dhs_nomiss$rural)
dhs_nomiss <- mutate(dhs_nomiss, 
              stratumid = str_c(state_dist_str, rural_str, sep = "_")) 
dhs_nomiss$stratumid <- as.factor(dhs_nomiss$stratumid)


##diabetic as numeric

dhs_nomiss <- mutate(dhs_nomiss,
              ex_diab_broad_ind_dbl = as.numeric(dhs_nomiss$ex_diab_broad_ind)-1)


####Check mean, median, 25th, 75th, min and max  in 1) a district, and 2) a PSU, if ex_diab_narrow_ind !=NA 

####check districts



#uniquestates<-unique(dhs_nomiss$ex_state_ind)

#keeptrack4<-NULL

#for(state in (uniquestates)){
 
#  keeptrack4<-c(keeptrack4,sum(dhs_nomiss$ex_state_ind==state))
#}
#names(keeptrack4)<-uniquestates
#summary(keeptrack4)

####check psus


#uniquepsu<-unique(dhs_nomiss_noNAinpsu$psu)

#keeptrack4<-NULL

#for(dis in (uniquepsu)){
  
  #keeptrack4<-c(keeptrack4,sum(dhs_nomiss_noNAinpsu$psu==dis))
#}
#names(keeptrack4)<-uniquepsu
#summary(keeptrack4)



####Create PSUS for subsets besides dhs, always if working with psu, use dataset with NAinpsu filtered for psu==NA

####NAinpsu for dhs_nomiss

dhs_nomiss <- mutate(dhs_nomiss, 
                     psu = ifelse(psu==1, NA, psu))

summary(dhs_nomiss$psu)


dhs_nomiss_noNAinpsu <- filter(dhs_nomiss, is.na(psu)==F)

summary(dhs_nomiss_noNAinpsu$psu)



###create PSU ID in dhs_nomiss:
dhs_nomiss <- dhs_nomiss %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss$psuid <- as.factor(dhs_nomiss$psuid) 





  dhs_nomiss_rural <- filter(dhs_nomiss, (urban_lab)=="Rural")
  dhs_nomiss_urban <- filter(dhs_nomiss, (urban_lab)=="Urban")

  
 ####make new regions

dhs_nomiss <- mutate(dhs_nomiss, 
                  # Nothern
                  zone_new = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir" | ex_state_ind=="Uttarakhand", "North",
                                          # Northeastern
                                          ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                 # Central
                                                 ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh"  | ex_state_ind== "Uttar Pradesh", "Central",
                                                        # Eastern
                                                        ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                               # Western
                                                               ifelse(ex_state_ind=="Daman and Diu"  | ex_state_ind=="Dadra and Nagar Haveli"| ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                      # Southern
                                                                      ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala"  | ex_state_ind=="Lakshadweep" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA)))))))) 
  
  
  
  

```




```{r MAP ALL Morbidities}

#  SET-UP  #


# http://stackoverflow.com/questions/28322866/mapping-just-one-state-of-india-and-writing-its-name-inside-the-state-boundary
library(rgeos)
library(rgdal)
library(raster) # get data for maps

india <- getData("GADM", country = "India", level = 1)
dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1)
india <- gSimplify(india, tol=0.01, topologyPreserve=TRUE) # this drastically reduces the detail in the GADM file to allow for decently quick plotting
map <- fortify(india) # makes a dataset out of a spatial object
map$id <- as.integer(map$id) # that's just to be able to dhse it

#dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1) # This is just a df of state names and state IDs
dat <- filter(dat, 
              row_number() != 31) # This just removes the Tamil Nadu duplicate (for some reason the india spatial data has a separate row for Madras as for TN)


centers <- data.frame(gCentroid(india, byid = TRUE)) # a df of latitude and longitude
centers <- filter(centers, 
                  row_number() !=31)  # This is removing the Tamil Nadu duplicate
centers$state <- as.factor(dat$state)  # adding state names to it
centers <- as_tibble(centers)

# Abbreviating the state names and throwing out Lakshadweep and Dadra, Nagar Haveli, D&D, Chandigarh, and Puducherry

centers <- centers %>% 
  mutate(state = fct_recode(state, 
                                     "HP" = "Himachal Pradesh",
                                     "PB" = "Punjab",
                                     "Chandigarh" = "Chandigarh",
                                     "HR" = "Haryana",
                                     "DL" = "NCT of Delhi",
                                     "SK" = "Sikkim",
                                     "Daman and Diu" = "Daman and Diu",
                                     "AR" = "Arunachal Pradesh",
                                     "NL" = "Nagaland",
                                     "MN" = "Manipur",
                                     "MZ" = "Mizoram",
                                     "TR" = "Tripura",
                                     "ML" = "Meghalaya",
                                     "WB" = "West Bengal",
                                     "MH" = "Maharashtra",
                                     "AP" = "Andhra Pradesh",
                                     "KA" = "Karnataka",
                                     "GA" = "Goa",
                                     "KL" = "Kerala",
                                     "Puducherry" = "Puducherry",
                                     "TN" = "Tamil Nadu",
                                     "AN" = "Andaman and Nicobar",
                                     "TS" = "Telangana",
                                     "UK" = "Uttarakhand",
                                     "RJ" = "Rajasthan",
                                     "UP" = "Uttar Pradesh",
                                     "BR" = "Bihar",
                                     "AS" = "Assam",
                                     "JH" = "Jharkhand",
                                     "OD" = "Odisha",
                                     "CT" = "Chhattisgarh", 
                                     "MP" = "Madhya Pradesh",
                                     "JK" = "Jammu and Kashmir",
                                     "GJ" = "Gujarat",
                                     "Lakshadweep" = "Lakshadweep",
                                "Dadra and Nagar Haveli" = "Dadra and Nagar Haveli")) %>%
                  filter(state != "Lakshadweep" & state != "Dadra and Nagar Haveli" & state != "Chandigarh" & state != "Daman and Diu" & state != "Puducherry" )

centers <- centers %>% 
  mutate(ex_state_ind = state)

theme_map <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace% 
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),  # deprecated
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.background=element_rect(fill="white", colour=NA),
legend.key=element_rect(colour="white"),
legend.key.size=unit(1.5, "lines"),
legend.position="right",
legend.text=element_text(size=15, family="Times"),
legend.title=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),  # deprecated
plot.background=element_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=element_text(size=rel(1.8), face="bold", hjust=0.5, family="Times"),
strip.background=element_rect(fill="white", colour="white"),
strip.text=element_text(size=rel(1.4), face="italic", family="Times")
)   
}


# Now calculate prevalence by state
temp.dat2 <- dhs_nomiss %>% 
  group_by(ex_state_ind) %>%
  mutate(multi_risc = 100*weighted.mean(multi_morbid_dbl,sworld_weight_india, na.rm=TRUE)) %>% 
  filter(row_number()==1) %>% 
  dplyr::select(ex_state_ind, multi_risc) %>% 
  filter(ex_state_ind!="Daman and Diu")  # Daman and Diu has a crazy high urban prev, so to not distort color scale kick out this invisible state


dat <- dat %>% 
  mutate(ex_state_ind = state) %>%
   mutate(ex_state_ind = fct_recode(ex_state_ind, 
                                     "Andaman and Nicobar Islands" = "Andaman and Nicobar",
                                     "Delhi" = "NCT of Delhi")) %>%
  filter(ex_state_ind!="Daman and Diu") 


dat$ex_state_ind <- factor(dat$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))

temp.dat2$ex_state_ind <- factor(temp.dat2$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))


map.dat2 <- left_join(dat, temp.dat2, by="ex_state_ind") # adds an id column to cvd_tempdat
map.dat2 <- inner_join(map, map.dat2, by = "id")


# Now plot the actual map - htn
htn_map2 <- ggplot() +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group),
         color = "#ffffff", fill = "#ececec", size = 0.25) +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group, fill=multi_risc),
         color = "#ffffff", size = 0.25) +
  geom_text_repel(data = centers, 
                  aes(label = ex_state_ind, x = x, y = y, fontface=2), 
                  size = 7, segment.color = "black", segment.size = 0.3, family="Times") +
  coord_map() +
  scale_fill_distiller(palette = "OrRd", direction = 1, na.value = "grey80") +
  labs(x = "", y = "") +
  xlim(68, 98) + 
  ylim(7, 35) +
  ggtitle("Prevalence of ≥ 2 morbidities (%)") + 
  theme_map()
htn_map2

```



```{r MAP CVD Morbidities}

#  SET-UP  #


# http://stackoverflow.com/questions/28322866/mapping-just-one-state-of-india-and-writing-its-name-inside-the-state-boundary
library(rgeos)
library(rgdal)
library(raster) # get data for maps

india <- getData("GADM", country = "India", level = 1)
dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1)
india <- gSimplify(india, tol=0.01, topologyPreserve=TRUE) # this drastically reduces the detail in the GADM file to allow for decently quick plotting
map <- fortify(india) # makes a dataset out of a spatial object
map$id <- as.integer(map$id) # that's just to be able to merge it

#dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1) # This is just a df of state names and state IDs
dat <- filter(dat, 
              row_number() != 31) # This just removes the Tamil Nadu duplicate (for some reason the india spatial data has a separate row for Madras as for TN)


centers <- data.frame(gCentroid(india, byid = TRUE)) # a df of latitude and longitude
centers <- filter(centers, 
                  row_number() !=31)  # This is removing the Tamil Nadu duplicate
centers$state <- as.factor(dat$state)  # adding state names to it
centers <- as_tibble(centers)

# Abbreviating the state names and throwing out Lakshadweep and Dadra, Nagar Haveli, D&D, Chandigarh, and Puducherry

centers <- centers %>% 
  mutate(state = fct_recode(state, 
                                     "HP" = "Himachal Pradesh",
                                     "PB" = "Punjab",
                                     "Chandigarh" = "Chandigarh",
                                     "HR" = "Haryana",
                                     "DL" = "NCT of Delhi",
                                     "SK" = "Sikkim",
                                     "Daman and Diu" = "Daman and Diu",
                                     "AR" = "Arunachal Pradesh",
                                     "NL" = "Nagaland",
                                     "MN" = "Manipur",
                                     "MZ" = "Mizoram",
                                     "TR" = "Tripura",
                                     "ML" = "Meghalaya",
                                     "WB" = "West Bengal",
                                     "MH" = "Maharashtra",
                                     "AP" = "Andhra Pradesh",
                                     "KA" = "Karnataka",
                                     "GA" = "Goa",
                                     "KL" = "Kerala",
                                     "Puducherry" = "Puducherry",
                                     "TN" = "Tamil Nadu",
                                     "AN" = "Andaman and Nicobar",
                                     "TS" = "Telangana",
                                     "UK" = "Uttarakhand",
                                     "RJ" = "Rajasthan",
                                     "UP" = "Uttar Pradesh",
                                     "BR" = "Bihar",
                                     "AS" = "Assam",
                                     "JH" = "Jharkhand",
                                     "OD" = "Odisha",
                                     "CT" = "Chhattisgarh", 
                                     "MP" = "Madhya Pradesh",
                                     "JK" = "Jammu and Kashmir",
                                     "GJ" = "Gujarat",
                                     "Lakshadweep" = "Lakshadweep",
                                "Dadra and Nagar Haveli" = "Dadra and Nagar Haveli")) %>%
                  filter(state != "Lakshadweep" & state != "Dadra and Nagar Haveli" & state != "Chandigarh" & state != "Daman and Diu" & state != "Puducherry" )

centers <- centers %>% 
  mutate(ex_state_ind = state)

theme_map <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace% 
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),  # deprecated
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.background=element_rect(fill="white", colour=NA),
legend.key=element_rect(colour="white"),
legend.key.size=unit(1.5, "lines"),
legend.position="right",
legend.text=element_text(size=15, family="Times"),
legend.title=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),  # deprecated
plot.background=element_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=element_text(size=rel(1.8), face="bold", hjust=0.5, family="Times"),
strip.background=element_rect(fill="white", colour="white"),
strip.text=element_text(size=rel(1.4), face="italic", family="Times")
)   
}


# Now calculate prevalence by state
temp.dat2 <- merg %>% 
  group_by(ex_state_ind) %>%
  mutate(multi_CVD_morb = 100*weighted.mean(multi_CVD_morb_dbl,sworld_weight_india, na.rm=TRUE)) %>% 
  filter(row_number()==1) %>% 
  dplyr::select(ex_state_ind, multi_CVD_morb) %>% 
  filter(ex_state_ind!="Daman and Diu")  # Daman and Diu has a crazy high urban prev, so to not distort color scale kick out this invisible state


dat <- dat %>% 
  mutate(ex_state_ind = state) %>%
   mutate(ex_state_ind = fct_recode(ex_state_ind, 
                                     "Andaman and Nicobar Islands" = "Andaman and Nicobar",
                                     "Delhi" = "NCT of Delhi")) %>%
  filter(ex_state_ind!="Daman and Diu") 


dat$ex_state_ind <- factor(dat$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))

temp.dat2$ex_state_ind <- factor(temp.dat2$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))


map.dat2 <- left_join(dat, temp.dat2, by="ex_state_ind") # adds an id column to cvd_tempdat
map.dat2 <- inner_join(map, map.dat2, by = "id")


# Now plot the actual map - htn
htn_map2 <- ggplot() +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group),
         color = "#ffffff", fill = "#ececec", size = 0.25) +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group, fill=multi_CVD_morb),
         color = "#ffffff", size = 0.25) +
  geom_text_repel(data = centers, 
                  aes(label = ex_state_ind, x = x, y = y, fontface=2), 
                  size = 7, segment.color = "black", segment.size = 0.3, family="Times") +
  coord_map() +
  scale_fill_distiller(palette = "OrRd", direction = 1, na.value = "grey80") +
  labs(x = "", y = "") +
  xlim(68, 98) + 
  ylim(7, 35) +
  ggtitle("Prevalence of ≥ 2 CVD morbidities (%)") + 
  theme_map()
htn_map2

```


```{r overall prevalence of at least one, at least two at least three cvd or ALL risk}

####ALL RISC


merg <-merg %>%
  mutate(age_grp_merg =ifelse(age<40, 0,
                                         ifelse(age>39,1,NA)))


 svy_ex_htn_narrow_ind <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp_merg))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    summarize(multi_risc_dbl_pop = survey_mean(age_grp_merg, proportion=TRUE, vartype = "ci")*100)
  write_csv(prevtot, "Prevalence multirisc by age group.csv")

####CVD RISC

morethan1cvd <- filter(merg, multi_CVD_risc_dbl==1)


morethan1cvd <-morethan1cvd %>%
  mutate(age_grp_morethan1cvd =ifelse(age<40, 0,
                                         ifelse(age>39,1,NA)))


 svy_ex_htn_narrow_ind <- morethan1cvd %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp_morethan1cvd))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    summarize(multi_risc_dbl_pop = survey_mean(age_grp_morethan1cvd, proportion=TRUE, vartype = "ci"))
  write_csv(prevtot, "Prevalence multi CVD risc by age group.csv")


```

```{r  older and younger 39 of the ones with >1}



####CVD morb

morethan1cvd <- filter(merg, sum_CVD_morb_dbl>=2)


morethan1cvd <-morethan1cvd %>%
  mutate(age_grp_morethan1cvd =ifelse(age<40, 0,
                                         ifelse(age>39,1,NA)))


 svy_ex_htn_narrow_ind <- morethan1cvd %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp_morethan1cvd))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    summarize(multi_morb_dbl_pop = survey_mean(age_grp_morethan1cvd, proportion=TRUE, vartype = "ci"))
  write_csv(prevtot, "Prevalence multi CVD morb by age group.csv")

  
  

```



```{r prevalence of over 2 among individuals with over 1 }


  ###CVD RISK

merg <- mutate(merg, 
                    zero_CVD_morb = ifelse(sum_CVD_morb_dbl==0,1,0),
                    one_CVD_morb = ifelse(sum_CVD_morb_dbl>=1,1,0),
                     two_CVD_morb = ifelse(sum_CVD_morb_dbl>=2,1,0),
                     three_CVD_morb = ifelse(sum_CVD_morb_dbl>=3,1,0),
                     four_CVD_morb = ifelse(sum_CVD_morb_dbl>=4,1,0),
                     five_CVD_morb = ifelse(sum_CVD_morb_dbl>=5,1,0),
                     six_CVD_morb = ifelse(sum_CVD_morb_dbl>=6,1,0),
                     seven_CVD_morb = ifelse(sum_CVD_morb_dbl>=7,1,0),
                     eight_CVD_morb = ifelse(sum_CVD_morb_dbl>=8,1,0))

over1cvd <- filter(merg, one_CVD_morb==1)

 svy_over1cvdrisk <- over1cvd %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( two_CVD_morb))
  
  prevtotover1cvdrisk <- svy_over1cvdrisk %>%
    summarize( two_CVD_morb_pop = survey_mean(two_CVD_morb, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotover1cvdrisk, "prevalence among over 1 of over 2 cvd risk.csv")



```



```{r prevalence of over 2 among 40-49 year old }



merg4049 <- filter(merg, age>39 & age<50)
  ###CVD RISK

merg4049 <- mutate(merg4049, 
                    zero_CVD_morb = ifelse(sum_CVD_morb_dbl==0,1,0),
                    one_CVD_morb = ifelse(sum_CVD_morb_dbl>=1,1,0),
                     two_CVD_morb = ifelse(sum_CVD_morb_dbl>=2,1,0),
                     three_CVD_morb = ifelse(sum_CVD_morb_dbl>=3,1,0),
                     four_CVD_morb = ifelse(sum_CVD_morb_dbl>=4,1,0),
                     five_CVD_morb = ifelse(sum_CVD_morb_dbl>=5,1,0),
                     six_CVD_morb = ifelse(sum_CVD_morb_dbl>=6,1,0),
                     seven_CVD_morb = ifelse(sum_CVD_morb_dbl>=7,1,0),
                     eight_CVD_morb = ifelse(sum_CVD_morb_dbl>=8,1,0))



 svy_merg4049 <- merg4049 %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( two_CVD_morb))
  
  prevtotover1cvdrisk <- svy_merg4049 %>%
    summarize( two_CVD_morb_pop = survey_mean(two_CVD_morb, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotover1cvdrisk, "prevalence of over 2 among individuals aged 40-49.csv")



```




```{r prevalence of over 2 among poorest}



mergpoorest <- filter(merg, wealth_quintile_rurb_lab== "Q1 (Poorest)")
  ###CVD RISK

mergpoorest <- mutate(mergpoorest, 
                    zero_CVD_morb = ifelse(sum_CVD_morb_dbl==0,1,0),
                    one_CVD_morb = ifelse(sum_CVD_morb_dbl>=1,1,0),
                     two_CVD_morb = ifelse(sum_CVD_morb_dbl>=2,1,0),
                     three_CVD_morb = ifelse(sum_CVD_morb_dbl>=3,1,0),
                     four_CVD_morb = ifelse(sum_CVD_morb_dbl>=4,1,0),
                     five_CVD_morb = ifelse(sum_CVD_morb_dbl>=5,1,0),
                     six_CVD_morb = ifelse(sum_CVD_morb_dbl>=6,1,0),
                     seven_CVD_morb = ifelse(sum_CVD_morb_dbl>=7,1,0),
                     eight_CVD_morb = ifelse(sum_CVD_morb_dbl>=8,1,0))



 svy_mergpoorest <- mergpoorest %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( two_CVD_morb))
  
  prevtotover1cvdrisk <- svy_mergpoorest %>%
    summarize( two_CVD_morb_pop = survey_mean(two_CVD_morb, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotover1cvdrisk, "prevalence of over 2 among poorest.csv")



```




```{r OVERALL prevalence of individuals with at least one metabolic syndrome factor }


  ###CVD RISK

merg <- mutate(merg, 
                    zero_CVD_morb = ifelse(sum_CVD_morb_dbl==0,1,0),
                    one_CVD_morb = ifelse(sum_CVD_morb_dbl>=1,1,0),
                     two_CVD_morb = ifelse(sum_CVD_morb_dbl>=2,1,0),
                     three_CVD_morb = ifelse(sum_CVD_morb_dbl>=3,1,0))

 svy_overallcvdrisk <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( zero_CVD_morb, one_CVD_morb, two_CVD_morb, three_CVD_morb))
  
  prevtotoverallcvdrisk <- svy_overallcvdrisk %>%
    summarize(zero_CVD_morb_pop = survey_mean(zero_CVD_morb, proportion=TRUE, vartype = "ci")*100,
              one_CVD_morb_pop = survey_mean(one_CVD_morb, proportion=TRUE, vartype = "ci")*100,
              two_CVD_morb_pop = survey_mean(two_CVD_morb, proportion=TRUE, vartype = "ci")*100,
              three_CVD_morb_pop = survey_mean(three_CVD_morb, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotoverallcvdrisk, "OVERALL prevalence multiCVD_morb.csv")



```



```{r OVERALL prevalence by sex}

###ALL RISK

merg <- mutate(merg, 
                    zero_risc = ifelse(sum_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_risc_dbl>=8,1,0))

 svy_overallrisksex <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(sex, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc))
  
  prevtotoverallrisksex <- svy_overallrisksex %>%
    group_by(sex)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotoverallrisksex, "OVERALL prevalence multirisc by sex.csv")
  
  ###CVD RISK

merg <- mutate(merg, 
                    zero_CVD_risc = ifelse(sum_CVD_risc_dbl==0,1,0),
                    one_CVD_risc = ifelse(sum_CVD_risc_dbl>=1,1,0),
                     two_CVD_risc = ifelse(sum_CVD_risc_dbl>=2,1,0),
                     three_CVD_risc = ifelse(sum_CVD_risc_dbl>=3,1,0),
                     four_CVD_risc = ifelse(sum_CVD_risc_dbl>=4,1,0),
                     five_CVD_risc = ifelse(sum_CVD_risc_dbl>=5,1,0),
                     six_CVD_risc = ifelse(sum_CVD_risc_dbl>=6,1,0),
                     seven_CVD_risc = ifelse(sum_CVD_risc_dbl>=7,1,0),
                     eight_CVD_risc = ifelse(sum_CVD_risc_dbl>=8,1,0))

 svy_overallcvdrisksex <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( zero_CVD_risc, one_CVD_risc, two_CVD_risc, three_CVD_risc, four_CVD_risc))
  
  prevtotoverallcvdrisksex <- svy_overallcvdrisksex %>%
    group_by(sex)%>%
    summarize(zero_CVD_risc_pop = survey_mean(zero_CVD_risc, proportion=TRUE, vartype = "ci")*100,
              one_CVD_risc_pop = survey_mean(one_CVD_risc, proportion=TRUE, vartype = "ci")*100,
              two_CVD_risc_pop = survey_mean(two_CVD_risc, proportion=TRUE, vartype = "ci")*100,
              three_CVD_risc_pop = survey_mean(three_CVD_risc, proportion=TRUE, vartype = "ci")*100,
              four_CVD_risc_pop = survey_mean(four_CVD_risc, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotoverallcvdrisksex, "OVERALL prevalence multiCVD_risc by sex.csv")



```



```{r OVERALL prevalence by urban rural}

###ALL RISK

merg <- mutate(merg, 
                    zero_risc = ifelse(sum_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_risc_dbl>=8,1,0))

 svy_overallriskurban <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(urban, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc, six_risc))
  
  prevtotoverallriskurban <- svy_overallriskurban %>%
    group_by(urban)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100,
              six_risc_pop = survey_mean(six_risc, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotoverallriskurban, "OVERALL prevalence multirisc by urban.csv")
  
  ###CVD RISK

merg <- mutate(merg, 
                    zero_CVD_risc = ifelse(sum_CVD_risc_dbl==0,1,0),
                    one_CVD_risc = ifelse(sum_CVD_risc_dbl>=1,1,0),
                     two_CVD_risc = ifelse(sum_CVD_risc_dbl>=2,1,0),
                     three_CVD_risc = ifelse(sum_CVD_risc_dbl>=3,1,0),
                     four_CVD_risc = ifelse(sum_CVD_risc_dbl>=4,1,0),
                     five_CVD_risc = ifelse(sum_CVD_risc_dbl>=5,1,0),
                     six_CVD_risc = ifelse(sum_CVD_risc_dbl>=6,1,0),
                     seven_CVD_risc = ifelse(sum_CVD_risc_dbl>=7,1,0),
                     eight_CVD_risc = ifelse(sum_CVD_risc_dbl>=8,1,0))

 svy_overallcvdriskurban <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( zero_CVD_risc, one_CVD_risc, two_CVD_risc, three_CVD_risc, four_CVD_risc, five_CVD_risc, six_CVD_risc))
  
  prevtotoverallcvdriskurban <- svy_overallcvdriskurban %>%
    group_by(urban)%>%
    summarize(zero_CVD_risc_pop = survey_mean(zero_CVD_risc, proportion=TRUE, vartype = "ci")*100,
              one_CVD_risc_pop = survey_mean(one_CVD_risc, proportion=TRUE, vartype = "ci")*100,
              two_CVD_risc_pop = survey_mean(two_CVD_risc, proportion=TRUE, vartype = "ci")*100,
              three_CVD_risc_pop = survey_mean(three_CVD_risc, proportion=TRUE, vartype = "ci")*100,
              four_CVD_risc_pop = survey_mean(four_CVD_risc, proportion=TRUE, vartype = "ci")*100,
              five_CVD_risc_pop = survey_mean(five_CVD_risc, proportion=TRUE, vartype = "ci")*100,
              six_CVD_risc_pop = survey_mean(six_CVD_risc, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotoverallcvdriskurban, "OVERALL prevalence multiCVD_risc by urban.csv")



```



```{r ALL risc and CVD risc prevalence per state}


  
  
   svy_ex_htn_narrow_ind <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(ex_state_ind,multi_CVD_morb_dbl))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(ex_state_ind)%>%
    summarize(multi_CVD_morb_dbl_pop = survey_mean(multi_CVD_morb_dbl, proportion=TRUE, vartype = "ci")*100)
  write_csv(prevtot, "CVD morb prevalence by state.csv")




```




```{r ALL risc and CVD risc prevalence per zone_new}


 svy_ex_htn_narrow_ind <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(zone_new,multi_risc_dbl))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(zone_new)%>%
    summarize(multi_risc_dbl_pop = survey_mean(multi_risc_dbl, proportion=TRUE, vartype = "ci")*100)
  write_csv(prevtot, "ALL risc prevalence by zone_new.csv")
  
  
   svy_ex_htn_narrow_ind <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(zone_new,multi_CVD_risc_dbl))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(zone_new)%>%
    summarize(multi_CVD_risc_dbl_pop = survey_mean(multi_CVD_risc_dbl, proportion=TRUE, vartype = "ci")*100)
  write_csv(prevtot, "CVD risc prevalence by zone_new.csv")




```




```{r prevalence 10 year age group 1,2,3,4 risc factors}



merg <- mutate(merg, 
                    zero_risc = ifelse(sum_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_risc_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc, six_risc))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100,
              six_risc_pop = survey_mean(six_risc, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtot, "10 year age group prevalence multirisc.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.multirisc` <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence multirisc.csv")
aware <- `10.year.age.group.prevalence.multirisc`



aware <- mutate(aware,
                zero_risc_pop = round(zero_risc_pop,2),
                one_risc_pop = round(one_risc_pop,2),
                two_risc_pop = round(two_risc_pop,2),
                three_risc_pop = round(three_risc_pop,2),
                four_risc_pop = round(four_risc_pop,2),
                five_risc_pop = round(five_risc_pop,2),
                six_risc_pop = round(six_risc_pop,2),
                zero_risc_pop_low = round(zero_risc_pop_low,2),
                one_risc_pop_low = round(one_risc_pop_low,2),
                two_risc_pop_low = round(two_risc_pop_low,2),
                three_risc_pop_low = round(three_risc_pop_low,2),
                four_risc_pop_low = round(four_risc_pop_low,2),
                five_risc_pop_low = round(five_risc_pop_low,2),
                six_risc_pop_low = round(six_risc_pop_low,2),
                zero_risc_pop_upp = round(zero_risc_pop_upp,2),
                one_risc_pop_upp = round(one_risc_pop_upp,2),
                two_risc_pop_upp = round(two_risc_pop_upp,2),
                three_risc_pop_upp = round(three_risc_pop_upp,2),
                four_risc_pop_upp = round(four_risc_pop_upp,2),
                five_risc_pop_upp = round(five_risc_pop_upp,2),
                six_risc_pop_upp = round(six_risc_pop_upp,2))


aware$zero_risc_pop <- sprintf("%.2f", aware$zero_risc_pop)
aware$one_risc_pop <- sprintf("%.2f", aware$one_risc_pop)
aware$two_risc_pop <- sprintf("%.2f", aware$two_risc_pop)
aware$three_risc_pop <- sprintf("%.2f", aware$three_risc_pop)
aware$four_risc_pop <- sprintf("%.2f", aware$four_risc_pop)
aware$five_risc_pop <- sprintf("%.2f", aware$five_risc_pop)
aware$six_risc_pop <- sprintf("%.2f", aware$six_risc_pop)
aware$zero_risc_pop_low <- sprintf("%.2f", aware$zero_risc_pop_low)
aware$one_risc_pop_low <- sprintf("%.2f", aware$one_risc_pop_low)
aware$two_risc_pop_low <- sprintf("%.2f", aware$two_risc_pop_low)
aware$three_risc_pop_low <- sprintf("%.2f", aware$three_risc_pop_low)
aware$four_risc_pop_low <- sprintf("%.2f", aware$four_risc_pop_low)
aware$five_risc_pop_low <- sprintf("%.2f", aware$five_risc_pop_low)
aware$six_risc_pop_low <- sprintf("%.2f", aware$six_risc_pop_low)
aware$zero_risc_pop_upp <- sprintf("%.2f", aware$zero_risc_pop_upp)
aware$one_risc_pop_upp <- sprintf("%.2f", aware$one_risc_pop_upp)
aware$two_risc_pop_upp <- sprintf("%.2f", aware$two_risc_pop_upp)
aware$three_risc_pop_upp <- sprintf("%.2f", aware$three_risc_pop_upp)
aware$four_risc_pop_upp <- sprintf("%.2f", aware$four_risc_pop_upp)
aware$five_risc_pop_upp <- sprintf("%.2f", aware$five_risc_pop_upp)
aware$six_risc_pop_upp <- sprintf("%.2f", aware$six_risc_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_pop_low, zero_risc_pop_upp, sep="-"),
                citempone = str_c(one_risc_pop_low, one_risc_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_pop_low, two_risc_pop_upp, sep="-"),
                citempthree = str_c(three_risc_pop_low, three_risc_pop_upp, sep="-"),
                citempfour = str_c(four_risc_pop_low, four_risc_pop_upp, sep="-"),
                citempfive = str_c(five_risc_pop_low, five_risc_pop_upp, sep="-"),
                citempsix = str_c(six_risc_pop_low, six_risc_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_pop, cizero, sep=" "),
                rrone = str_c(one_risc_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_pop, cisix, sep=" "))




write.csv(aware, "10 year prev risc.csv")

```




```{r prevalence 10 year age group ALL risc factor DHS and DLHS/AHS separately}

DLHS_AHS <- filter(merg, svy=="AHS" | svy=="DLHS")


DLHS_AHS <- filter(DLHS_AHS, age>=18 & age<50)

DLHS_AHS <- mutate(DLHS_AHS, 
                    zero_risc = ifelse(sum_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_risc_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- DLHS_AHS %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc, six_risc))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100,
              six_risc_pop = survey_mean(six_risc, proportion=TRUE, vartype = "ci")*100) 
  write.csv(prevtot, "10 year age group prevalence multirisc DLHS_AHS only.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.multirisc` <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence multirisc DLHS_AHS only.csv")
aware <- `10.year.age.group.prevalence.multirisc`



aware <- mutate(aware,
                zero_risc_pop = round(zero_risc_pop,2),
                one_risc_pop = round(one_risc_pop,2),
                two_risc_pop = round(two_risc_pop,2),
                three_risc_pop = round(three_risc_pop,2),
                four_risc_pop = round(four_risc_pop,2),
                five_risc_pop = round(five_risc_pop,2),
                six_risc_pop = round(six_risc_pop,2),
                zero_risc_pop_low = round(zero_risc_pop_low,2),
                one_risc_pop_low = round(one_risc_pop_low,2),
                two_risc_pop_low = round(two_risc_pop_low,2),
                three_risc_pop_low = round(three_risc_pop_low,2),
                four_risc_pop_low = round(four_risc_pop_low,2),
                five_risc_pop_low = round(five_risc_pop_low,2),
                six_risc_pop_low = round(six_risc_pop_low,2),
                zero_risc_pop_upp = round(zero_risc_pop_upp,2),
                one_risc_pop_upp = round(one_risc_pop_upp,2),
                two_risc_pop_upp = round(two_risc_pop_upp,2),
                three_risc_pop_upp = round(three_risc_pop_upp,2),
                four_risc_pop_upp = round(four_risc_pop_upp,2),
                five_risc_pop_upp = round(five_risc_pop_upp,2),
                six_risc_pop_upp = round(six_risc_pop_upp,2))


aware$zero_risc_pop <- sprintf("%.2f", aware$zero_risc_pop)
aware$one_risc_pop <- sprintf("%.2f", aware$one_risc_pop)
aware$two_risc_pop <- sprintf("%.2f", aware$two_risc_pop)
aware$three_risc_pop <- sprintf("%.2f", aware$three_risc_pop)
aware$four_risc_pop <- sprintf("%.2f", aware$four_risc_pop)
aware$five_risc_pop <- sprintf("%.2f", aware$five_risc_pop)
aware$six_risc_pop <- sprintf("%.2f", aware$six_risc_pop)
aware$zero_risc_pop_low <- sprintf("%.2f", aware$zero_risc_pop_low)
aware$one_risc_pop_low <- sprintf("%.2f", aware$one_risc_pop_low)
aware$two_risc_pop_low <- sprintf("%.2f", aware$two_risc_pop_low)
aware$three_risc_pop_low <- sprintf("%.2f", aware$three_risc_pop_low)
aware$four_risc_pop_low <- sprintf("%.2f", aware$four_risc_pop_low)
aware$five_risc_pop_low <- sprintf("%.2f", aware$five_risc_pop_low)
aware$six_risc_pop_low <- sprintf("%.2f", aware$six_risc_pop_low)
aware$zero_risc_pop_upp <- sprintf("%.2f", aware$zero_risc_pop_upp)
aware$one_risc_pop_upp <- sprintf("%.2f", aware$one_risc_pop_upp)
aware$two_risc_pop_upp <- sprintf("%.2f", aware$two_risc_pop_upp)
aware$three_risc_pop_upp <- sprintf("%.2f", aware$three_risc_pop_upp)
aware$four_risc_pop_upp <- sprintf("%.2f", aware$four_risc_pop_upp)
aware$five_risc_pop_upp <- sprintf("%.2f", aware$five_risc_pop_upp)
aware$six_risc_pop_upp <- sprintf("%.2f", aware$six_risc_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_pop_low, zero_risc_pop_upp, sep="-"),
                citempone = str_c(one_risc_pop_low, one_risc_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_pop_low, two_risc_pop_upp, sep="-"),
                citempthree = str_c(three_risc_pop_low, three_risc_pop_upp, sep="-"),
                citempfour = str_c(four_risc_pop_low, four_risc_pop_upp, sep="-"),
                citempfive = str_c(five_risc_pop_low, five_risc_pop_upp, sep="-"),
                citempsix = str_c(six_risc_pop_low, six_risc_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_pop, cizero, sep=" "),
                rrone = str_c(one_risc_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_pop, cisix, sep=" "))




write.csv(aware, "10 year prev risc DLHS_AHS only.csv")





DHS <- filter(merg, svy=="DHS" )

DHS <- filter(DHS, age>=18 & age<50)

DHS <- mutate(DHS, 
                    zero_risc = ifelse(sum_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_risc_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- DHS %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc, six_risc))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100,
              six_risc_pop = survey_mean(six_risc, proportion=TRUE, vartype = "ci")*100) 
  write.csv(prevtot, "10 year age group prevalence multirisc DHS only.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.multirisc` <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence multirisc DHS only.csv")
aware <- `10.year.age.group.prevalence.multirisc`



aware <- mutate(aware,
                zero_risc_pop = round(zero_risc_pop,2),
                one_risc_pop = round(one_risc_pop,2),
                two_risc_pop = round(two_risc_pop,2),
                three_risc_pop = round(three_risc_pop,2),
                four_risc_pop = round(four_risc_pop,2),
                five_risc_pop = round(five_risc_pop,2),
                six_risc_pop = round(six_risc_pop,2),
                zero_risc_pop_low = round(zero_risc_pop_low,2),
                one_risc_pop_low = round(one_risc_pop_low,2),
                two_risc_pop_low = round(two_risc_pop_low,2),
                three_risc_pop_low = round(three_risc_pop_low,2),
                four_risc_pop_low = round(four_risc_pop_low,2),
                five_risc_pop_low = round(five_risc_pop_low,2),
                six_risc_pop_low = round(six_risc_pop_low,2),
                zero_risc_pop_upp = round(zero_risc_pop_upp,2),
                one_risc_pop_upp = round(one_risc_pop_upp,2),
                two_risc_pop_upp = round(two_risc_pop_upp,2),
                three_risc_pop_upp = round(three_risc_pop_upp,2),
                four_risc_pop_upp = round(four_risc_pop_upp,2),
                five_risc_pop_upp = round(five_risc_pop_upp,2),
                six_risc_pop_upp = round(six_risc_pop_upp,2))


aware$zero_risc_pop <- sprintf("%.2f", aware$zero_risc_pop)
aware$one_risc_pop <- sprintf("%.2f", aware$one_risc_pop)
aware$two_risc_pop <- sprintf("%.2f", aware$two_risc_pop)
aware$three_risc_pop <- sprintf("%.2f", aware$three_risc_pop)
aware$four_risc_pop <- sprintf("%.2f", aware$four_risc_pop)
aware$five_risc_pop <- sprintf("%.2f", aware$five_risc_pop)
aware$six_risc_pop <- sprintf("%.2f", aware$six_risc_pop)
aware$zero_risc_pop_low <- sprintf("%.2f", aware$zero_risc_pop_low)
aware$one_risc_pop_low <- sprintf("%.2f", aware$one_risc_pop_low)
aware$two_risc_pop_low <- sprintf("%.2f", aware$two_risc_pop_low)
aware$three_risc_pop_low <- sprintf("%.2f", aware$three_risc_pop_low)
aware$four_risc_pop_low <- sprintf("%.2f", aware$four_risc_pop_low)
aware$five_risc_pop_low <- sprintf("%.2f", aware$five_risc_pop_low)
aware$six_risc_pop_low <- sprintf("%.2f", aware$six_risc_pop_low)
aware$zero_risc_pop_upp <- sprintf("%.2f", aware$zero_risc_pop_upp)
aware$one_risc_pop_upp <- sprintf("%.2f", aware$one_risc_pop_upp)
aware$two_risc_pop_upp <- sprintf("%.2f", aware$two_risc_pop_upp)
aware$three_risc_pop_upp <- sprintf("%.2f", aware$three_risc_pop_upp)
aware$four_risc_pop_upp <- sprintf("%.2f", aware$four_risc_pop_upp)
aware$five_risc_pop_upp <- sprintf("%.2f", aware$five_risc_pop_upp)
aware$six_risc_pop_upp <- sprintf("%.2f", aware$six_risc_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_pop_low, zero_risc_pop_upp, sep="-"),
                citempone = str_c(one_risc_pop_low, one_risc_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_pop_low, two_risc_pop_upp, sep="-"),
                citempthree = str_c(three_risc_pop_low, three_risc_pop_upp, sep="-"),
                citempfour = str_c(four_risc_pop_low, four_risc_pop_upp, sep="-"),
                citempfive = str_c(five_risc_pop_low, five_risc_pop_upp, sep="-"),
                citempsix = str_c(six_risc_pop_low, six_risc_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_pop, cizero, sep=" "),
                rrone = str_c(one_risc_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_pop, cisix, sep=" "))




write.csv(aware, "10 year prev risc DHS only.csv")

```




```{r prevalence 10 year age group all risc MEN vs women}

merg_men <- filter(merg, sex==0)


merg_men <- mutate(merg_men, 
                    zero_risc = ifelse(sum_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_risc_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- merg_men %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc, six_risc))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100,
              six_risc_pop = survey_mean(six_risc, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtot, "10 year age group prevalence multirisc men.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.multirisc` <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence multirisc men.csv")
aware <- `10.year.age.group.prevalence.multirisc`



aware <- mutate(aware,
                zero_risc_pop = round(zero_risc_pop,2),
                one_risc_pop = round(one_risc_pop,2),
                two_risc_pop = round(two_risc_pop,2),
                three_risc_pop = round(three_risc_pop,2),
                four_risc_pop = round(four_risc_pop,2),
                five_risc_pop = round(five_risc_pop,2),
                six_risc_pop = round(six_risc_pop,2),
                zero_risc_pop_low = round(zero_risc_pop_low,2),
                one_risc_pop_low = round(one_risc_pop_low,2),
                two_risc_pop_low = round(two_risc_pop_low,2),
                three_risc_pop_low = round(three_risc_pop_low,2),
                four_risc_pop_low = round(four_risc_pop_low,2),
                five_risc_pop_low = round(five_risc_pop_low,2),
                six_risc_pop_low = round(six_risc_pop_low,2),
                zero_risc_pop_upp = round(zero_risc_pop_upp,2),
                one_risc_pop_upp = round(one_risc_pop_upp,2),
                two_risc_pop_upp = round(two_risc_pop_upp,2),
                three_risc_pop_upp = round(three_risc_pop_upp,2),
                four_risc_pop_upp = round(four_risc_pop_upp,2),
                five_risc_pop_upp = round(five_risc_pop_upp,2),
                six_risc_pop_upp = round(six_risc_pop_upp,2))


aware$zero_risc_pop <- sprintf("%.2f", aware$zero_risc_pop)
aware$one_risc_pop <- sprintf("%.2f", aware$one_risc_pop)
aware$two_risc_pop <- sprintf("%.2f", aware$two_risc_pop)
aware$three_risc_pop <- sprintf("%.2f", aware$three_risc_pop)
aware$four_risc_pop <- sprintf("%.2f", aware$four_risc_pop)
aware$five_risc_pop <- sprintf("%.2f", aware$five_risc_pop)
aware$six_risc_pop <- sprintf("%.2f", aware$six_risc_pop)
aware$zero_risc_pop_low <- sprintf("%.2f", aware$zero_risc_pop_low)
aware$one_risc_pop_low <- sprintf("%.2f", aware$one_risc_pop_low)
aware$two_risc_pop_low <- sprintf("%.2f", aware$two_risc_pop_low)
aware$three_risc_pop_low <- sprintf("%.2f", aware$three_risc_pop_low)
aware$four_risc_pop_low <- sprintf("%.2f", aware$four_risc_pop_low)
aware$five_risc_pop_low <- sprintf("%.2f", aware$five_risc_pop_low)
aware$six_risc_pop_low <- sprintf("%.2f", aware$six_risc_pop_low)
aware$zero_risc_pop_upp <- sprintf("%.2f", aware$zero_risc_pop_upp)
aware$one_risc_pop_upp <- sprintf("%.2f", aware$one_risc_pop_upp)
aware$two_risc_pop_upp <- sprintf("%.2f", aware$two_risc_pop_upp)
aware$three_risc_pop_upp <- sprintf("%.2f", aware$three_risc_pop_upp)
aware$four_risc_pop_upp <- sprintf("%.2f", aware$four_risc_pop_upp)
aware$five_risc_pop_upp <- sprintf("%.2f", aware$five_risc_pop_upp)
aware$six_risc_pop_upp <- sprintf("%.2f", aware$six_risc_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_pop_low, zero_risc_pop_upp, sep="-"),
                citempone = str_c(one_risc_pop_low, one_risc_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_pop_low, two_risc_pop_upp, sep="-"),
                citempthree = str_c(three_risc_pop_low, three_risc_pop_upp, sep="-"),
                citempfour = str_c(four_risc_pop_low, four_risc_pop_upp, sep="-"),
                citempfive = str_c(five_risc_pop_low, five_risc_pop_upp, sep="-"),
                citempsix = str_c(six_risc_pop_low, six_risc_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_pop, cizero, sep=" "),
                rrone = str_c(one_risc_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_pop, cisix, sep=" "))




write.csv(aware, "10 year prev risc men.csv")




merg_women <- filter(merg, sex==1)


merg_women <- mutate(merg_women, 
                    zero_risc = ifelse(sum_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_risc_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- merg_women %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc, six_risc))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100,
              six_risc_pop = survey_mean(six_risc, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtot, "10 year age group prevalence multirisc women.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.multirisc` <- read.csv("~/Docuwoments/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence multirisc women.csv")
aware <- `10.year.age.group.prevalence.multirisc`



aware <- mutate(aware,
                zero_risc_pop = round(zero_risc_pop,2),
                one_risc_pop = round(one_risc_pop,2),
                two_risc_pop = round(two_risc_pop,2),
                three_risc_pop = round(three_risc_pop,2),
                four_risc_pop = round(four_risc_pop,2),
                five_risc_pop = round(five_risc_pop,2),
                six_risc_pop = round(six_risc_pop,2),
                zero_risc_pop_low = round(zero_risc_pop_low,2),
                one_risc_pop_low = round(one_risc_pop_low,2),
                two_risc_pop_low = round(two_risc_pop_low,2),
                three_risc_pop_low = round(three_risc_pop_low,2),
                four_risc_pop_low = round(four_risc_pop_low,2),
                five_risc_pop_low = round(five_risc_pop_low,2),
                six_risc_pop_low = round(six_risc_pop_low,2),
                zero_risc_pop_upp = round(zero_risc_pop_upp,2),
                one_risc_pop_upp = round(one_risc_pop_upp,2),
                two_risc_pop_upp = round(two_risc_pop_upp,2),
                three_risc_pop_upp = round(three_risc_pop_upp,2),
                four_risc_pop_upp = round(four_risc_pop_upp,2),
                five_risc_pop_upp = round(five_risc_pop_upp,2),
                six_risc_pop_upp = round(six_risc_pop_upp,2))


aware$zero_risc_pop <- sprintf("%.2f", aware$zero_risc_pop)
aware$one_risc_pop <- sprintf("%.2f", aware$one_risc_pop)
aware$two_risc_pop <- sprintf("%.2f", aware$two_risc_pop)
aware$three_risc_pop <- sprintf("%.2f", aware$three_risc_pop)
aware$four_risc_pop <- sprintf("%.2f", aware$four_risc_pop)
aware$five_risc_pop <- sprintf("%.2f", aware$five_risc_pop)
aware$six_risc_pop <- sprintf("%.2f", aware$six_risc_pop)
aware$zero_risc_pop_low <- sprintf("%.2f", aware$zero_risc_pop_low)
aware$one_risc_pop_low <- sprintf("%.2f", aware$one_risc_pop_low)
aware$two_risc_pop_low <- sprintf("%.2f", aware$two_risc_pop_low)
aware$three_risc_pop_low <- sprintf("%.2f", aware$three_risc_pop_low)
aware$four_risc_pop_low <- sprintf("%.2f", aware$four_risc_pop_low)
aware$five_risc_pop_low <- sprintf("%.2f", aware$five_risc_pop_low)
aware$six_risc_pop_low <- sprintf("%.2f", aware$six_risc_pop_low)
aware$zero_risc_pop_upp <- sprintf("%.2f", aware$zero_risc_pop_upp)
aware$one_risc_pop_upp <- sprintf("%.2f", aware$one_risc_pop_upp)
aware$two_risc_pop_upp <- sprintf("%.2f", aware$two_risc_pop_upp)
aware$three_risc_pop_upp <- sprintf("%.2f", aware$three_risc_pop_upp)
aware$four_risc_pop_upp <- sprintf("%.2f", aware$four_risc_pop_upp)
aware$five_risc_pop_upp <- sprintf("%.2f", aware$five_risc_pop_upp)
aware$six_risc_pop_upp <- sprintf("%.2f", aware$six_risc_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_pop_low, zero_risc_pop_upp, sep="-"),
                citempone = str_c(one_risc_pop_low, one_risc_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_pop_low, two_risc_pop_upp, sep="-"),
                citempthree = str_c(three_risc_pop_low, three_risc_pop_upp, sep="-"),
                citempfour = str_c(four_risc_pop_low, four_risc_pop_upp, sep="-"),
                citempfive = str_c(five_risc_pop_low, five_risc_pop_upp, sep="-"),
                citempsix = str_c(six_risc_pop_low, six_risc_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_pop, cizero, sep=" "),
                rrone = str_c(one_risc_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_pop, cisix, sep=" "))




write.csv(aware, "10 year prev risc women.csv")


```





```{r prevalence 10 year age group 1,2,3,4 CVD risc factors!!!}



merg <- mutate(merg, 
                    zero_risc = ifelse(sum_CVD_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_CVD_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_CVD_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_CVD_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_CVD_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_CVD_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_CVD_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_CVD_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_CVD_risc_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc, six_risc))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100,
              six_risc_pop = survey_mean(six_risc, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtot, "10 year age group prevalence multirisc.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.multirisc` <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence multirisc.csv")
aware <- `10.year.age.group.prevalence.multirisc`



aware <- mutate(aware,
                zero_risc_pop = round(zero_risc_pop,2),
                one_risc_pop = round(one_risc_pop,2),
                two_risc_pop = round(two_risc_pop,2),
                three_risc_pop = round(three_risc_pop,2),
                four_risc_pop = round(four_risc_pop,2),
                five_risc_pop = round(five_risc_pop,2),
                six_risc_pop = round(six_risc_pop,2),
                zero_risc_pop_low = round(zero_risc_pop_low,2),
                one_risc_pop_low = round(one_risc_pop_low,2),
                two_risc_pop_low = round(two_risc_pop_low,2),
                three_risc_pop_low = round(three_risc_pop_low,2),
                four_risc_pop_low = round(four_risc_pop_low,2),
                five_risc_pop_low = round(five_risc_pop_low,2),
                six_risc_pop_low = round(six_risc_pop_low,2),
                zero_risc_pop_upp = round(zero_risc_pop_upp,2),
                one_risc_pop_upp = round(one_risc_pop_upp,2),
                two_risc_pop_upp = round(two_risc_pop_upp,2),
                three_risc_pop_upp = round(three_risc_pop_upp,2),
                four_risc_pop_upp = round(four_risc_pop_upp,2),
                five_risc_pop_upp = round(five_risc_pop_upp,2),
                six_risc_pop_upp = round(six_risc_pop_upp,2))


aware$zero_risc_pop <- sprintf("%.2f", aware$zero_risc_pop)
aware$one_risc_pop <- sprintf("%.2f", aware$one_risc_pop)
aware$two_risc_pop <- sprintf("%.2f", aware$two_risc_pop)
aware$three_risc_pop <- sprintf("%.2f", aware$three_risc_pop)
aware$four_risc_pop <- sprintf("%.2f", aware$four_risc_pop)
aware$five_risc_pop <- sprintf("%.2f", aware$five_risc_pop)
aware$six_risc_pop <- sprintf("%.2f", aware$six_risc_pop)
aware$zero_risc_pop_low <- sprintf("%.2f", aware$zero_risc_pop_low)
aware$one_risc_pop_low <- sprintf("%.2f", aware$one_risc_pop_low)
aware$two_risc_pop_low <- sprintf("%.2f", aware$two_risc_pop_low)
aware$three_risc_pop_low <- sprintf("%.2f", aware$three_risc_pop_low)
aware$four_risc_pop_low <- sprintf("%.2f", aware$four_risc_pop_low)
aware$five_risc_pop_low <- sprintf("%.2f", aware$five_risc_pop_low)
aware$six_risc_pop_low <- sprintf("%.2f", aware$six_risc_pop_low)
aware$zero_risc_pop_upp <- sprintf("%.2f", aware$zero_risc_pop_upp)
aware$one_risc_pop_upp <- sprintf("%.2f", aware$one_risc_pop_upp)
aware$two_risc_pop_upp <- sprintf("%.2f", aware$two_risc_pop_upp)
aware$three_risc_pop_upp <- sprintf("%.2f", aware$three_risc_pop_upp)
aware$four_risc_pop_upp <- sprintf("%.2f", aware$four_risc_pop_upp)
aware$five_risc_pop_upp <- sprintf("%.2f", aware$five_risc_pop_upp)
aware$six_risc_pop_upp <- sprintf("%.2f", aware$six_risc_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_pop_low, zero_risc_pop_upp, sep="-"),
                citempone = str_c(one_risc_pop_low, one_risc_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_pop_low, two_risc_pop_upp, sep="-"),
                citempthree = str_c(three_risc_pop_low, three_risc_pop_upp, sep="-"),
                citempfour = str_c(four_risc_pop_low, four_risc_pop_upp, sep="-"),
                citempfive = str_c(five_risc_pop_low, five_risc_pop_upp, sep="-"),
                citempsix = str_c(six_risc_pop_low, six_risc_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_pop, cizero, sep=" "),
                rrone = str_c(one_risc_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_pop, cisix, sep=" "))




write.csv(aware, "10 year prev CVD risc!!!!!!.csv")

```




```{r prevalence 10 year age group CVD risc factor DHS and DLHS/AHS separately}

DLHS_AHS <- filter(merg, svy=="AHS" | svy=="DLHS")


DLHS_AHS <- filter(DLHS_AHS, age>=18 & age<50)

DLHS_AHS <- mutate(DLHS_AHS, 
                    zero_risc = ifelse(sum_CVD_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_CVD_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_CVD_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_CVD_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_CVD_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_CVD_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_CVD_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_CVD_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_CVD_risc_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- DLHS_AHS %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc, six_risc))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100,
              six_risc_pop = survey_mean(six_risc, proportion=TRUE, vartype = "ci")*100) 
  write.csv(prevtot, "10 year age group prevalence CVD risc DLHS_AHS only.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.multirisc` <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence CVD risc DLHS_AHS only.csv")
aware <- `10.year.age.group.prevalence.multirisc`



aware <- mutate(aware,
                zero_risc_pop = round(zero_risc_pop,2),
                one_risc_pop = round(one_risc_pop,2),
                two_risc_pop = round(two_risc_pop,2),
                three_risc_pop = round(three_risc_pop,2),
                four_risc_pop = round(four_risc_pop,2),
                five_risc_pop = round(five_risc_pop,2),
                six_risc_pop = round(six_risc_pop,2),
                zero_risc_pop_low = round(zero_risc_pop_low,2),
                one_risc_pop_low = round(one_risc_pop_low,2),
                two_risc_pop_low = round(two_risc_pop_low,2),
                three_risc_pop_low = round(three_risc_pop_low,2),
                four_risc_pop_low = round(four_risc_pop_low,2),
                five_risc_pop_low = round(five_risc_pop_low,2),
                six_risc_pop_low = round(six_risc_pop_low,2),
                zero_risc_pop_upp = round(zero_risc_pop_upp,2),
                one_risc_pop_upp = round(one_risc_pop_upp,2),
                two_risc_pop_upp = round(two_risc_pop_upp,2),
                three_risc_pop_upp = round(three_risc_pop_upp,2),
                four_risc_pop_upp = round(four_risc_pop_upp,2),
                five_risc_pop_upp = round(five_risc_pop_upp,2),
                six_risc_pop_upp = round(six_risc_pop_upp,2))


aware$zero_risc_pop <- sprintf("%.2f", aware$zero_risc_pop)
aware$one_risc_pop <- sprintf("%.2f", aware$one_risc_pop)
aware$two_risc_pop <- sprintf("%.2f", aware$two_risc_pop)
aware$three_risc_pop <- sprintf("%.2f", aware$three_risc_pop)
aware$four_risc_pop <- sprintf("%.2f", aware$four_risc_pop)
aware$five_risc_pop <- sprintf("%.2f", aware$five_risc_pop)
aware$six_risc_pop <- sprintf("%.2f", aware$six_risc_pop)
aware$zero_risc_pop_low <- sprintf("%.2f", aware$zero_risc_pop_low)
aware$one_risc_pop_low <- sprintf("%.2f", aware$one_risc_pop_low)
aware$two_risc_pop_low <- sprintf("%.2f", aware$two_risc_pop_low)
aware$three_risc_pop_low <- sprintf("%.2f", aware$three_risc_pop_low)
aware$four_risc_pop_low <- sprintf("%.2f", aware$four_risc_pop_low)
aware$five_risc_pop_low <- sprintf("%.2f", aware$five_risc_pop_low)
aware$six_risc_pop_low <- sprintf("%.2f", aware$six_risc_pop_low)
aware$zero_risc_pop_upp <- sprintf("%.2f", aware$zero_risc_pop_upp)
aware$one_risc_pop_upp <- sprintf("%.2f", aware$one_risc_pop_upp)
aware$two_risc_pop_upp <- sprintf("%.2f", aware$two_risc_pop_upp)
aware$three_risc_pop_upp <- sprintf("%.2f", aware$three_risc_pop_upp)
aware$four_risc_pop_upp <- sprintf("%.2f", aware$four_risc_pop_upp)
aware$five_risc_pop_upp <- sprintf("%.2f", aware$five_risc_pop_upp)
aware$six_risc_pop_upp <- sprintf("%.2f", aware$six_risc_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_pop_low, zero_risc_pop_upp, sep="-"),
                citempone = str_c(one_risc_pop_low, one_risc_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_pop_low, two_risc_pop_upp, sep="-"),
                citempthree = str_c(three_risc_pop_low, three_risc_pop_upp, sep="-"),
                citempfour = str_c(four_risc_pop_low, four_risc_pop_upp, sep="-"),
                citempfive = str_c(five_risc_pop_low, five_risc_pop_upp, sep="-"),
                citempsix = str_c(six_risc_pop_low, six_risc_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_pop, cizero, sep=" "),
                rrone = str_c(one_risc_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_pop, cisix, sep=" "))




write.csv(aware, "10 year prev CVD risc DLHS_AHS only.csv")





DHS <- filter(merg, svy=="DHS" )

DHS <- filter(DHS, age>=18 & age<50)

DHS <- mutate(DHS, 
                    zero_risc = ifelse(sum_CVD_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_CVD_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_CVD_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_CVD_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_CVD_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_CVD_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_CVD_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_CVD_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_CVD_risc_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- DHS %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc, six_risc))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100,
              six_risc_pop = survey_mean(six_risc, proportion=TRUE, vartype = "ci")*100) 
  write.csv(prevtot, "10 year age group prevalence CVD risc DHS only.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.multirisc` <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence CVD risc DHS only.csv")
aware <- `10.year.age.group.prevalence.multirisc`



aware <- mutate(aware,
                zero_risc_pop = round(zero_risc_pop,2),
                one_risc_pop = round(one_risc_pop,2),
                two_risc_pop = round(two_risc_pop,2),
                three_risc_pop = round(three_risc_pop,2),
                four_risc_pop = round(four_risc_pop,2),
                five_risc_pop = round(five_risc_pop,2),
                six_risc_pop = round(six_risc_pop,2),
                zero_risc_pop_low = round(zero_risc_pop_low,2),
                one_risc_pop_low = round(one_risc_pop_low,2),
                two_risc_pop_low = round(two_risc_pop_low,2),
                three_risc_pop_low = round(three_risc_pop_low,2),
                four_risc_pop_low = round(four_risc_pop_low,2),
                five_risc_pop_low = round(five_risc_pop_low,2),
                six_risc_pop_low = round(six_risc_pop_low,2),
                zero_risc_pop_upp = round(zero_risc_pop_upp,2),
                one_risc_pop_upp = round(one_risc_pop_upp,2),
                two_risc_pop_upp = round(two_risc_pop_upp,2),
                three_risc_pop_upp = round(three_risc_pop_upp,2),
                four_risc_pop_upp = round(four_risc_pop_upp,2),
                five_risc_pop_upp = round(five_risc_pop_upp,2),
                six_risc_pop_upp = round(six_risc_pop_upp,2))


aware$zero_risc_pop <- sprintf("%.2f", aware$zero_risc_pop)
aware$one_risc_pop <- sprintf("%.2f", aware$one_risc_pop)
aware$two_risc_pop <- sprintf("%.2f", aware$two_risc_pop)
aware$three_risc_pop <- sprintf("%.2f", aware$three_risc_pop)
aware$four_risc_pop <- sprintf("%.2f", aware$four_risc_pop)
aware$five_risc_pop <- sprintf("%.2f", aware$five_risc_pop)
aware$six_risc_pop <- sprintf("%.2f", aware$six_risc_pop)
aware$zero_risc_pop_low <- sprintf("%.2f", aware$zero_risc_pop_low)
aware$one_risc_pop_low <- sprintf("%.2f", aware$one_risc_pop_low)
aware$two_risc_pop_low <- sprintf("%.2f", aware$two_risc_pop_low)
aware$three_risc_pop_low <- sprintf("%.2f", aware$three_risc_pop_low)
aware$four_risc_pop_low <- sprintf("%.2f", aware$four_risc_pop_low)
aware$five_risc_pop_low <- sprintf("%.2f", aware$five_risc_pop_low)
aware$six_risc_pop_low <- sprintf("%.2f", aware$six_risc_pop_low)
aware$zero_risc_pop_upp <- sprintf("%.2f", aware$zero_risc_pop_upp)
aware$one_risc_pop_upp <- sprintf("%.2f", aware$one_risc_pop_upp)
aware$two_risc_pop_upp <- sprintf("%.2f", aware$two_risc_pop_upp)
aware$three_risc_pop_upp <- sprintf("%.2f", aware$three_risc_pop_upp)
aware$four_risc_pop_upp <- sprintf("%.2f", aware$four_risc_pop_upp)
aware$five_risc_pop_upp <- sprintf("%.2f", aware$five_risc_pop_upp)
aware$six_risc_pop_upp <- sprintf("%.2f", aware$six_risc_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_pop_low, zero_risc_pop_upp, sep="-"),
                citempone = str_c(one_risc_pop_low, one_risc_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_pop_low, two_risc_pop_upp, sep="-"),
                citempthree = str_c(three_risc_pop_low, three_risc_pop_upp, sep="-"),
                citempfour = str_c(four_risc_pop_low, four_risc_pop_upp, sep="-"),
                citempfive = str_c(five_risc_pop_low, five_risc_pop_upp, sep="-"),
                citempsix = str_c(six_risc_pop_low, six_risc_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_pop, cizero, sep=" "),
                rrone = str_c(one_risc_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_pop, cisix, sep=" "))




write.csv(aware, "10 year prev CVD risc DHS only.csv")

```




```{r prevalence 10 year age group CVD risc factors MEN vs WOmen}

merg_men <- filter(merg, sex==0)

merg_men <- mutate(merg_men, 
                    zero_risc = ifelse(sum_CVD_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_CVD_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_CVD_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_CVD_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_CVD_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_CVD_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_CVD_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_CVD_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_CVD_risc_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- merg_men %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc, six_risc))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100,
              six_risc_pop = survey_mean(six_risc, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtot, "10 year age group prevalence men multirisc.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.men` <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence men multirisc.csv")
aware <- `10.year.age.group.prevalence.men`



aware <- mutate(aware,
                zero_risc_pop = round(zero_risc_pop,2),
                one_risc_pop = round(one_risc_pop,2),
                two_risc_pop = round(two_risc_pop,2),
                three_risc_pop = round(three_risc_pop,2),
                four_risc_pop = round(four_risc_pop,2),
                five_risc_pop = round(five_risc_pop,2),
                six_risc_pop = round(six_risc_pop,2),
                zero_risc_pop_low = round(zero_risc_pop_low,2),
                one_risc_pop_low = round(one_risc_pop_low,2),
                two_risc_pop_low = round(two_risc_pop_low,2),
                three_risc_pop_low = round(three_risc_pop_low,2),
                four_risc_pop_low = round(four_risc_pop_low,2),
                five_risc_pop_low = round(five_risc_pop_low,2),
                six_risc_pop_low = round(six_risc_pop_low,2),
                zero_risc_pop_upp = round(zero_risc_pop_upp,2),
                one_risc_pop_upp = round(one_risc_pop_upp,2),
                two_risc_pop_upp = round(two_risc_pop_upp,2),
                three_risc_pop_upp = round(three_risc_pop_upp,2),
                four_risc_pop_upp = round(four_risc_pop_upp,2),
                five_risc_pop_upp = round(five_risc_pop_upp,2),
                six_risc_pop_upp = round(six_risc_pop_upp,2))


aware$zero_risc_pop <- sprintf("%.2f", aware$zero_risc_pop)
aware$one_risc_pop <- sprintf("%.2f", aware$one_risc_pop)
aware$two_risc_pop <- sprintf("%.2f", aware$two_risc_pop)
aware$three_risc_pop <- sprintf("%.2f", aware$three_risc_pop)
aware$four_risc_pop <- sprintf("%.2f", aware$four_risc_pop)
aware$five_risc_pop <- sprintf("%.2f", aware$five_risc_pop)
aware$six_risc_pop <- sprintf("%.2f", aware$six_risc_pop)
aware$zero_risc_pop_low <- sprintf("%.2f", aware$zero_risc_pop_low)
aware$one_risc_pop_low <- sprintf("%.2f", aware$one_risc_pop_low)
aware$two_risc_pop_low <- sprintf("%.2f", aware$two_risc_pop_low)
aware$three_risc_pop_low <- sprintf("%.2f", aware$three_risc_pop_low)
aware$four_risc_pop_low <- sprintf("%.2f", aware$four_risc_pop_low)
aware$five_risc_pop_low <- sprintf("%.2f", aware$five_risc_pop_low)
aware$six_risc_pop_low <- sprintf("%.2f", aware$six_risc_pop_low)
aware$zero_risc_pop_upp <- sprintf("%.2f", aware$zero_risc_pop_upp)
aware$one_risc_pop_upp <- sprintf("%.2f", aware$one_risc_pop_upp)
aware$two_risc_pop_upp <- sprintf("%.2f", aware$two_risc_pop_upp)
aware$three_risc_pop_upp <- sprintf("%.2f", aware$three_risc_pop_upp)
aware$four_risc_pop_upp <- sprintf("%.2f", aware$four_risc_pop_upp)
aware$five_risc_pop_upp <- sprintf("%.2f", aware$five_risc_pop_upp)
aware$six_risc_pop_upp <- sprintf("%.2f", aware$six_risc_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_pop_low, zero_risc_pop_upp, sep="-"),
                citempone = str_c(one_risc_pop_low, one_risc_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_pop_low, two_risc_pop_upp, sep="-"),
                citempthree = str_c(three_risc_pop_low, three_risc_pop_upp, sep="-"),
                citempfour = str_c(four_risc_pop_low, four_risc_pop_upp, sep="-"),
                citempfive = str_c(five_risc_pop_low, five_risc_pop_upp, sep="-"),
                citempsix = str_c(six_risc_pop_low, six_risc_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_pop, cizero, sep=" "),
                rrone = str_c(one_risc_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_pop, cisix, sep=" "))




write.csv(aware, "10 year prev CVD risc men!!!!!!.csv")




merg_women <- filter(merg, sex==1)

merg_women <- mutate(merg_women, 
                    zero_risc = ifelse(sum_CVD_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_CVD_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_CVD_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_CVD_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_CVD_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_CVD_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_CVD_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_CVD_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_CVD_risc_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- merg_women %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc, one_risc, two_risc, three_risc, four_risc, five_risc, six_risc))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_pop = survey_mean(zero_risc, proportion=TRUE, vartype = "ci")*100,
              one_risc_pop = survey_mean(one_risc, proportion=TRUE, vartype = "ci")*100,
              two_risc_pop = survey_mean(two_risc, proportion=TRUE, vartype = "ci")*100,
              three_risc_pop = survey_mean(three_risc, proportion=TRUE, vartype = "ci")*100,
              four_risc_pop = survey_mean(four_risc, proportion=TRUE, vartype = "ci")*100,
              five_risc_pop = survey_mean(five_risc, proportion=TRUE, vartype = "ci")*100,
              six_risc_pop = survey_mean(six_risc, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtot, "10 year age group prevalence women multirisc.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.women` <- read.csv("~/Docuwoments/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence women multirisc.csv")
aware <- `10.year.age.group.prevalence.women`



aware <- mutate(aware,
                zero_risc_pop = round(zero_risc_pop,2),
                one_risc_pop = round(one_risc_pop,2),
                two_risc_pop = round(two_risc_pop,2),
                three_risc_pop = round(three_risc_pop,2),
                four_risc_pop = round(four_risc_pop,2),
                five_risc_pop = round(five_risc_pop,2),
                six_risc_pop = round(six_risc_pop,2),
                zero_risc_pop_low = round(zero_risc_pop_low,2),
                one_risc_pop_low = round(one_risc_pop_low,2),
                two_risc_pop_low = round(two_risc_pop_low,2),
                three_risc_pop_low = round(three_risc_pop_low,2),
                four_risc_pop_low = round(four_risc_pop_low,2),
                five_risc_pop_low = round(five_risc_pop_low,2),
                six_risc_pop_low = round(six_risc_pop_low,2),
                zero_risc_pop_upp = round(zero_risc_pop_upp,2),
                one_risc_pop_upp = round(one_risc_pop_upp,2),
                two_risc_pop_upp = round(two_risc_pop_upp,2),
                three_risc_pop_upp = round(three_risc_pop_upp,2),
                four_risc_pop_upp = round(four_risc_pop_upp,2),
                five_risc_pop_upp = round(five_risc_pop_upp,2),
                six_risc_pop_upp = round(six_risc_pop_upp,2))


aware$zero_risc_pop <- sprintf("%.2f", aware$zero_risc_pop)
aware$one_risc_pop <- sprintf("%.2f", aware$one_risc_pop)
aware$two_risc_pop <- sprintf("%.2f", aware$two_risc_pop)
aware$three_risc_pop <- sprintf("%.2f", aware$three_risc_pop)
aware$four_risc_pop <- sprintf("%.2f", aware$four_risc_pop)
aware$five_risc_pop <- sprintf("%.2f", aware$five_risc_pop)
aware$six_risc_pop <- sprintf("%.2f", aware$six_risc_pop)
aware$zero_risc_pop_low <- sprintf("%.2f", aware$zero_risc_pop_low)
aware$one_risc_pop_low <- sprintf("%.2f", aware$one_risc_pop_low)
aware$two_risc_pop_low <- sprintf("%.2f", aware$two_risc_pop_low)
aware$three_risc_pop_low <- sprintf("%.2f", aware$three_risc_pop_low)
aware$four_risc_pop_low <- sprintf("%.2f", aware$four_risc_pop_low)
aware$five_risc_pop_low <- sprintf("%.2f", aware$five_risc_pop_low)
aware$six_risc_pop_low <- sprintf("%.2f", aware$six_risc_pop_low)
aware$zero_risc_pop_upp <- sprintf("%.2f", aware$zero_risc_pop_upp)
aware$one_risc_pop_upp <- sprintf("%.2f", aware$one_risc_pop_upp)
aware$two_risc_pop_upp <- sprintf("%.2f", aware$two_risc_pop_upp)
aware$three_risc_pop_upp <- sprintf("%.2f", aware$three_risc_pop_upp)
aware$four_risc_pop_upp <- sprintf("%.2f", aware$four_risc_pop_upp)
aware$five_risc_pop_upp <- sprintf("%.2f", aware$five_risc_pop_upp)
aware$six_risc_pop_upp <- sprintf("%.2f", aware$six_risc_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_pop_low, zero_risc_pop_upp, sep="-"),
                citempone = str_c(one_risc_pop_low, one_risc_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_pop_low, two_risc_pop_upp, sep="-"),
                citempthree = str_c(three_risc_pop_low, three_risc_pop_upp, sep="-"),
                citempfour = str_c(four_risc_pop_low, four_risc_pop_upp, sep="-"),
                citempfive = str_c(five_risc_pop_low, five_risc_pop_upp, sep="-"),
                citempsix = str_c(six_risc_pop_low, six_risc_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_pop, cizero, sep=" "),
                rrone = str_c(one_risc_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_pop, cisix, sep=" "))




write.csv(aware, "10 year prev CVD risc women!!!!!!.csv")


```




```{r age morb figure}


#####dummy variables for each number of risc factors

dhs_nomiss <- mutate(dhs_nomiss, 
                    zero_morb = ifelse(sum_multi_dbl==0,1,0),
                    one_morb = ifelse(sum_multi_dbl>=1,1,0),
                     two_morb = ifelse(sum_multi_dbl>=2,1,0),
                     three_morb = ifelse(sum_multi_dbl>=3,1,0),
                     four_morb = ifelse(sum_multi_dbl>=4,1,0),
                     five_morb = ifelse(sum_multi_dbl>=5,1,0),
                     six_morb = ifelse(sum_multi_dbl>=6,1,0),
                     seven_morb = ifelse(sum_multi_dbl>=7,1,0),
                     eight_morb = ifelse(sum_multi_dbl>=8,1,0))
               
               

dhs_nomisssmaller80 <- filter(dhs_nomiss, age<=49)

library(spatstat)
statemean.dat <- dhs_nomisssmaller80 %>%
  group_by(age) %>%
  mutate(zeromorb = weighted.mean(zero_morb, sworld_weight_india, na.rm=TRUE)*100,
         onemorb = weighted.mean(one_morb, sworld_weight_india, na.rm=TRUE)*100, 
         twomorb = weighted.mean(two_morb, sworld_weight_india, na.rm=TRUE)*100,
         threemorb = weighted.mean(three_morb, sworld_weight_india, na.rm=TRUE)*100,
         fourmorb = weighted.mean(four_morb, sworld_weight_india, na.rm=TRUE)*100,
         fivemorb = weighted.mean(five_morb, sworld_weight_india, na.rm=TRUE)*100,
         sixmorb =weighted.mean(six_morb, sworld_weight_india, na.rm=TRUE)*100,
         sevenmorb = weighted.mean(seven_morb, sworld_weight_india, na.rm=TRUE)*100,
         eightmorb = weighted.mean(eight_morb, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zeromorb, onemorb, twomorb, threemorb, fourmorb, fivemorb, sixmorb, sevenmorb, eightmorb)



#####Aware


stateawarefig <- statemean.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemean.dat$onemorb, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$twomorb, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$threemorb, x=statemean.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemean.dat$fourmorb, x=statemean.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$onemorb ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$twomorb ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$threemorb ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$fourmorb ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 morb factor"="firebrick1", ">=2 morb factor"="firebrick3", 
                                 ">=3 morb factor"="firebrick4", ">=4 morb factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20, 30, 40,49), limits=c(15, 49)) +
  coord_fixed(34/100, expand=F)
stateawarefig


```






```{r age risc figure separately for DLHS/AHS and DHS}



DLHS_AHS <- filter(merg, svy=="AHS" | svy=="DLHS")


DLHS_AHS <- filter(DLHS_AHS, age>=18 & age<50)


#####dummy variables for each number of risc factors

DLHS_AHS <- mutate(DLHS_AHS, 
                    zero_risc = ifelse(sum_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_risc_dbl>=8,1,0))
               
               


library(spatstat)
statemeanDLHS_AHS.dat <- DLHS_AHS %>%
  group_by(age) %>%
  mutate(zerorisc = weighted.mean(zero_risc, sworld_weight_india, na.rm=TRUE)*100,
         onerisc = weighted.mean(one_risc, sworld_weight_india, na.rm=TRUE)*100, 
         tworisc = weighted.mean(two_risc, sworld_weight_india, na.rm=TRUE)*100,
         threerisc = weighted.mean(three_risc, sworld_weight_india, na.rm=TRUE)*100,
         fourrisc = weighted.mean(four_risc, sworld_weight_india, na.rm=TRUE)*100,
         fiverisc = weighted.mean(five_risc, sworld_weight_india, na.rm=TRUE)*100,
         sixrisc =weighted.mean(six_risc, sworld_weight_india, na.rm=TRUE)*100,
         sevenrisc = weighted.mean(seven_risc, sworld_weight_india, na.rm=TRUE)*100,
         eightrisc = weighted.mean(eight_risc, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zerorisc, onerisc, tworisc, threerisc, fourrisc, fiverisc, sixrisc, sevenrisc, eightrisc)



#####Aware


stateawarefig <- statemeanDLHS_AHS.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemeanDLHS_AHS.dat$onerisc, x=statemeanDLHS_AHS.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanDLHS_AHS.dat$tworisc, x=statemeanDLHS_AHS.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanDLHS_AHS.dat$threerisc, x=statemeanDLHS_AHS.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemeanDLHS_AHS.dat$fourrisc, x=statemeanDLHS_AHS.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDLHS_AHS.dat$onerisc ~ statemeanDLHS_AHS.dat$age,span=1)),x=statemeanDLHS_AHS.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDLHS_AHS.dat$tworisc ~ statemeanDLHS_AHS.dat$age,span=1)),x=statemeanDLHS_AHS.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDLHS_AHS.dat$threerisc ~ statemeanDLHS_AHS.dat$age,span=1)),x=statemeanDLHS_AHS.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDLHS_AHS.dat$fourrisc ~ statemeanDLHS_AHS.dat$age,span=1)),x=statemeanDLHS_AHS.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 risc factor"="firebrick1", ">=2 risc factor"="firebrick3", 
                                 ">=3 risc factor"="firebrick4", ">=4 risc factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(18, 28, 38 ,49), limits=c(18, 49)) +
  coord_fixed(31/100, expand=F)
stateawarefig








DHS <- filter(merg, svy=="DHS" )


DHS <- filter(DHS, age>=18 & age<50)


#####dummy variables for each number of risc factors

DHS <- mutate(DHS, 
                    zero_risc = ifelse(sum_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_risc_dbl>=8,1,0))
               
               


library(spatstat)
statemeanDHS.dat <- DHS %>%
  group_by(age) %>%
  mutate(zerorisc = weighted.mean(zero_risc, sworld_weight_india, na.rm=TRUE)*100,
         onerisc = weighted.mean(one_risc, sworld_weight_india, na.rm=TRUE)*100, 
         tworisc = weighted.mean(two_risc, sworld_weight_india, na.rm=TRUE)*100,
         threerisc = weighted.mean(three_risc, sworld_weight_india, na.rm=TRUE)*100,
         fourrisc = weighted.mean(four_risc, sworld_weight_india, na.rm=TRUE)*100,
         fiverisc = weighted.mean(five_risc, sworld_weight_india, na.rm=TRUE)*100,
         sixrisc =weighted.mean(six_risc, sworld_weight_india, na.rm=TRUE)*100,
         sevenrisc = weighted.mean(seven_risc, sworld_weight_india, na.rm=TRUE)*100,
         eightrisc = weighted.mean(eight_risc, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zerorisc, onerisc, tworisc, threerisc, fourrisc, fiverisc, sixrisc, sevenrisc, eightrisc)



#####Aware


stateawarefig <- statemeanDHS.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemeanDHS.dat$onerisc, x=statemeanDHS.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanDHS.dat$tworisc, x=statemeanDHS.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanDHS.dat$threerisc, x=statemeanDHS.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemeanDHS.dat$fourrisc, x=statemeanDHS.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDHS.dat$onerisc ~ statemeanDHS.dat$age,span=1)),x=statemeanDHS.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDHS.dat$tworisc ~ statemeanDHS.dat$age,span=1)),x=statemeanDHS.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDHS.dat$threerisc ~ statemeanDHS.dat$age,span=1)),x=statemeanDHS.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDHS.dat$fourrisc ~ statemeanDHS.dat$age,span=1)),x=statemeanDHS.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 risc factor"="firebrick1", ">=2 risc factor"="firebrick3", 
                                 ">=3 risc factor"="firebrick4", ">=4 risc factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
   scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(18, 28, 38 ,49), limits=c(18, 49)) +
  coord_fixed(31/100, expand=F)
stateawarefig







```



```{r age risc figure men vs women}


merg_men <- filter(merg, sex==0)

#####dummy variables for each number of risc factors

merg_men <- mutate(merg_men, 
                    zero_risc = ifelse(sum_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_risc_dbl>=8,1,0))
               
               

merg_mensmaller80 <- filter(merg_men, age<=80)

library(spatstat)
statemeanmenallrisc.dat <- merg_mensmaller80 %>%
  group_by(age) %>%
  mutate(zerorisc = weighted.mean(zero_risc, sworld_weight_india, na.rm=TRUE)*100,
         onerisc = weighted.mean(one_risc, sworld_weight_india, na.rm=TRUE)*100, 
         tworisc = weighted.mean(two_risc, sworld_weight_india, na.rm=TRUE)*100,
         threerisc = weighted.mean(three_risc, sworld_weight_india, na.rm=TRUE)*100,
         fourrisc = weighted.mean(four_risc, sworld_weight_india, na.rm=TRUE)*100,
         fiverisc = weighted.mean(five_risc, sworld_weight_india, na.rm=TRUE)*100,
         sixrisc =weighted.mean(six_risc, sworld_weight_india, na.rm=TRUE)*100,
         sevenrisc = weighted.mean(seven_risc, sworld_weight_india, na.rm=TRUE)*100,
         eightrisc = weighted.mean(eight_risc, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zerorisc, onerisc, tworisc, threerisc, fourrisc, fiverisc, sixrisc, sevenrisc, eightrisc)



#####Aware


stateawarefig <- statemeanmenallrisc.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemeanmenallrisc.dat$onerisc, x=statemeanmenallrisc.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanmenallrisc.dat$tworisc, x=statemeanmenallrisc.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanmenallrisc.dat$threerisc, x=statemeanmenallrisc.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemeanmenallrisc.dat$fourrisc, x=statemeanmenallrisc.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmenallrisc.dat$onerisc ~ statemeanmenallrisc.dat$age,span=1)),x=statemeanmenallrisc.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmenallrisc.dat$tworisc ~ statemeanmenallrisc.dat$age,span=1)),x=statemeanmenallrisc.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmenallrisc.dat$threerisc ~ statemeanmenallrisc.dat$age,span=1)),x=statemeanmenallrisc.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmenallrisc.dat$fourrisc ~ statemeanmenallrisc.dat$age,span=1)),x=statemeanmenallrisc.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 risc factor"="firebrick1", ">=2 risc factor"="firebrick3", 
                                 ">=3 risc factor"="firebrick4", ">=4 risc factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20, 40, 60,80), limits=c(15, 80)) +
  coord_fixed(65/100, expand=F)
stateawarefig






merg_women <- filter(merg, sex==1)

#####dummy variables for each number of risc factors

merg_women <- mutate(merg_women, 
                    zero_risc = ifelse(sum_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_risc_dbl>=8,1,0))
               
               

merg_womensmaller80 <- filter(merg_women, age<=80)

library(spatstat)
statemeanwomenallrisc.dat <- merg_womensmaller80 %>%
  group_by(age) %>%
  mutate(zerorisc = weighted.mean(zero_risc, sworld_weight_india, na.rm=TRUE)*100,
         onerisc = weighted.mean(one_risc, sworld_weight_india, na.rm=TRUE)*100, 
         tworisc = weighted.mean(two_risc, sworld_weight_india, na.rm=TRUE)*100,
         threerisc = weighted.mean(three_risc, sworld_weight_india, na.rm=TRUE)*100,
         fourrisc = weighted.mean(four_risc, sworld_weight_india, na.rm=TRUE)*100,
         fiverisc = weighted.mean(five_risc, sworld_weight_india, na.rm=TRUE)*100,
         sixrisc =weighted.mean(six_risc, sworld_weight_india, na.rm=TRUE)*100,
         sevenrisc = weighted.mean(seven_risc, sworld_weight_india, na.rm=TRUE)*100,
         eightrisc = weighted.mean(eight_risc, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zerorisc, onerisc, tworisc, threerisc, fourrisc, fiverisc, sixrisc, sevenrisc, eightrisc)



#####Aware


stateawarefig <- statemeanwomenallrisc.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemeanwomenallrisc.dat$onerisc, x=statemeanwomenallrisc.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanwomenallrisc.dat$tworisc, x=statemeanwomenallrisc.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanwomenallrisc.dat$threerisc, x=statemeanwomenallrisc.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemeanwomenallrisc.dat$fourrisc, x=statemeanwomenallrisc.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanwomenallrisc.dat$onerisc ~ statemeanwomenallrisc.dat$age,span=1)),x=statemeanwomenallrisc.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanwomenallrisc.dat$tworisc ~ statemeanwomenallrisc.dat$age,span=1)),x=statemeanwomenallrisc.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanwomenallrisc.dat$threerisc ~ statemeanwomenallrisc.dat$age,span=1)),x=statemeanwomenallrisc.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanwomenallrisc.dat$fourrisc ~ statemeanwomenallrisc.dat$age,span=1)),x=statemeanwomenallrisc.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 risc factor"="firebrick1", ">=2 risc factor"="firebrick3", 
                                 ">=3 risc factor"="firebrick4", ">=4 risc factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20, 40, 60,80), limits=c(15, 80)) +
  coord_fixed(65/100, expand=F)
stateawarefig



```



```{r age CVD morb figure!!!!!!!!}


#####dummy variables for each number of morb factors

merg <- mutate(merg, 
                    zero_morb = ifelse(sum_CVD_morb_dbl==0,1,0),
                    one_morb = ifelse(sum_CVD_morb_dbl>=1,1,0),
                     two_morb = ifelse(sum_CVD_morb_dbl>=2,1,0),
                     three_morb = ifelse(sum_CVD_morb_dbl>=3,1,0),
                     four_morb = ifelse(sum_CVD_morb_dbl>=4,1,0),
                     five_morb = ifelse(sum_CVD_morb_dbl>=5,1,0),
                     six_morb = ifelse(sum_CVD_morb_dbl>=6,1,0),
                     seven_morb = ifelse(sum_CVD_morb_dbl>=7,1,0),
                     eight_morb = ifelse(sum_CVD_morb_dbl>=8,1,0))
               
               

mergsmaller80 <- filter(merg, age<=80)

library(spatstat)
statemean.dat <- mergsmaller80 %>%
  group_by(age) %>%
  mutate(zeromorb = weighted.mean(zero_morb, sworld_weight_india, na.rm=TRUE)*100,
         onemorb = weighted.mean(one_morb, sworld_weight_india, na.rm=TRUE)*100, 
         twomorb = weighted.mean(two_morb, sworld_weight_india, na.rm=TRUE)*100,
         threemorb = weighted.mean(three_morb, sworld_weight_india, na.rm=TRUE)*100,
         fourmorb = weighted.mean(four_morb, sworld_weight_india, na.rm=TRUE)*100,
         fivemorb = weighted.mean(five_morb, sworld_weight_india, na.rm=TRUE)*100,
         sixmorb =weighted.mean(six_morb, sworld_weight_india, na.rm=TRUE)*100,
         sevenmorb = weighted.mean(seven_morb, sworld_weight_india, na.rm=TRUE)*100,
         eightmorb = weighted.mean(eight_morb, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zeromorb, onemorb, twomorb, threemorb, fourmorb, fivemorb, sixmorb, sevenmorb, eightmorb)



#####Aware


stateawarefig <- statemean.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemean.dat$onemorb, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$twomorb, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$threemorb, x=statemean.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemean.dat$fourmorb, x=statemean.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$onemorb ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$twomorb ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$threemorb ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$fourmorb ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 morb factor"="firebrick1", ">=2 morb factor"="firebrick3", 
                                 ">=3 morb factor"="firebrick4", ">=4 morb factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20, 40, 60,80), limits=c(18, 80)) +
  coord_fixed(62/100, expand=F)
stateawarefig


```





```{r age CVD risc figure women vs men!!!!!!!!}


merg_men <- filter(merg, sex==0)



#####dummy variables for each number of risc factors

merg_men <- mutate(merg_men, 
                    zero_risc = ifelse(sum_CVD_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_CVD_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_CVD_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_CVD_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_CVD_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_CVD_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_CVD_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_CVD_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_CVD_risc_dbl>=8,1,0))
               
               

merg_mensmaller80 <- filter(merg_men, age<=80)

library(spatstat)
statemeanmenCVDrisc.dat <- merg_mensmaller80 %>%
  group_by(age) %>%
  mutate(zerorisc = weighted.mean(zero_risc, sworld_weight_india, na.rm=TRUE)*100,
         onerisc = weighted.mean(one_risc, sworld_weight_india, na.rm=TRUE)*100, 
         tworisc = weighted.mean(two_risc, sworld_weight_india, na.rm=TRUE)*100,
         threerisc = weighted.mean(three_risc, sworld_weight_india, na.rm=TRUE)*100,
         fourrisc = weighted.mean(four_risc, sworld_weight_india, na.rm=TRUE)*100,
         fiverisc = weighted.mean(five_risc, sworld_weight_india, na.rm=TRUE)*100,
         sixrisc =weighted.mean(six_risc, sworld_weight_india, na.rm=TRUE)*100,
         sevenrisc = weighted.mean(seven_risc, sworld_weight_india, na.rm=TRUE)*100,
         eightrisc = weighted.mean(eight_risc, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zerorisc, onerisc, tworisc, threerisc, fourrisc, fiverisc, sixrisc, sevenrisc, eightrisc)



#####Aware


stateawarefig <- statemeanmenCVDrisc.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemeanmenCVDrisc.dat$onerisc, x=statemeanmenCVDrisc.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanmenCVDrisc.dat$tworisc, x=statemeanmenCVDrisc.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanmenCVDrisc.dat$threerisc, x=statemeanmenCVDrisc.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemeanmenCVDrisc.dat$fourrisc, x=statemeanmenCVDrisc.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmenCVDrisc.dat$onerisc ~ statemeanmenCVDrisc.dat$age,span=1)),x=statemeanmenCVDrisc.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmenCVDrisc.dat$tworisc ~ statemeanmenCVDrisc.dat$age,span=1)),x=statemeanmenCVDrisc.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmenCVDrisc.dat$threerisc ~ statemeanmenCVDrisc.dat$age,span=1)),x=statemeanmenCVDrisc.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmenCVDrisc.dat$fourrisc ~ statemeanmenCVDrisc.dat$age,span=1)),x=statemeanmenCVDrisc.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 risc factor"="firebrick1", ">=2 risc factor"="firebrick3", 
                                 ">=3 risc factor"="firebrick4", ">=4 risc factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20, 40, 60,80), limits=c(15, 80)) +
  coord_fixed(65/100, expand=F)
stateawarefig



merg_women <- filter(merg, sex==1)



#####dummy variables for each number of risc factors

merg_women <- mutate(merg_women, 
                    zero_risc = ifelse(sum_CVD_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_CVD_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_CVD_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_CVD_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_CVD_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_CVD_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_CVD_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_CVD_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_CVD_risc_dbl>=8,1,0))
               
               

merg_womensmaller80 <- filter(merg_women, age<=80)

library(spatstat)
statemeanwomenCVDrisc.dat <- merg_womensmaller80 %>%
  group_by(age) %>%
  mutate(zerorisc = weighted.mean(zero_risc, sworld_weight_india, na.rm=TRUE)*100,
         onerisc = weighted.mean(one_risc, sworld_weight_india, na.rm=TRUE)*100, 
         tworisc = weighted.mean(two_risc, sworld_weight_india, na.rm=TRUE)*100,
         threerisc = weighted.mean(three_risc, sworld_weight_india, na.rm=TRUE)*100,
         fourrisc = weighted.mean(four_risc, sworld_weight_india, na.rm=TRUE)*100,
         fiverisc = weighted.mean(five_risc, sworld_weight_india, na.rm=TRUE)*100,
         sixrisc =weighted.mean(six_risc, sworld_weight_india, na.rm=TRUE)*100,
         sevenrisc = weighted.mean(seven_risc, sworld_weight_india, na.rm=TRUE)*100,
         eightrisc = weighted.mean(eight_risc, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zerorisc, onerisc, tworisc, threerisc, fourrisc, fiverisc, sixrisc, sevenrisc, eightrisc)



#####Aware


stateawarefig <- statemeanwomenCVDrisc.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemean.dat$onerisc, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$tworisc, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$threerisc, x=statemean.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemean.dat$fourrisc, x=statemean.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanwomenCVDrisc.dat$onerisc ~ statemeanwomenCVDrisc.dat$age,span=1)),x=statemeanwomenCVDrisc.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanwomenCVDrisc.dat$tworisc ~ statemeanwomenCVDrisc.dat$age,span=1)),x=statemeanwomenCVDrisc.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanwomenCVDrisc.dat$threerisc ~ statemeanwomenCVDrisc.dat$age,span=1)),x=statemeanwomenCVDrisc.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanwomenCVDrisc.dat$fourrisc ~ statemeanwomenCVDrisc.dat$age,span=1)),x=statemeanwomenCVDrisc.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 risc factor"="firebrick1", ">=2 risc factor"="firebrick3", 
                                 ">=3 risc factor"="firebrick4", ">=4 risc factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20, 40, 60,80), limits=c(15, 80)) +
  coord_fixed(65/100, expand=F)
stateawarefig







```



```{r age CVD risc figure separately for DLHS/AHS and DHS}



DLHS_AHS <- filter(merg, svy=="AHS" | svy=="DLHS")


DLHS_AHS <- filter(DLHS_AHS, age>=18 & age<50)


#####dummy variables for each number of risc factors

DLHS_AHS <- mutate(DLHS_AHS, 
                    zero_risc = ifelse(sum_CVD_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_CVD_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_CVD_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_CVD_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_CVD_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_CVD_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_CVD_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_CVD_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_CVD_risc_dbl>=8,1,0))
               
               


library(spatstat)
statemeanDLHS_AHS.dat <- DLHS_AHS %>%
  group_by(age) %>%
  mutate(zerorisc = weighted.mean(zero_risc, sworld_weight_india, na.rm=TRUE)*100,
         onerisc = weighted.mean(one_risc, sworld_weight_india, na.rm=TRUE)*100, 
         tworisc = weighted.mean(two_risc, sworld_weight_india, na.rm=TRUE)*100,
         threerisc = weighted.mean(three_risc, sworld_weight_india, na.rm=TRUE)*100,
         fourrisc = weighted.mean(four_risc, sworld_weight_india, na.rm=TRUE)*100,
         fiverisc = weighted.mean(five_risc, sworld_weight_india, na.rm=TRUE)*100,
         sixrisc =weighted.mean(six_risc, sworld_weight_india, na.rm=TRUE)*100,
         sevenrisc = weighted.mean(seven_risc, sworld_weight_india, na.rm=TRUE)*100,
         eightrisc = weighted.mean(eight_risc, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zerorisc, onerisc, tworisc, threerisc, fourrisc, fiverisc, sixrisc, sevenrisc, eightrisc)



#####Aware


stateawarefig <- statemeanDLHS_AHS.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemeanDLHS_AHS.dat$onerisc, x=statemeanDLHS_AHS.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanDLHS_AHS.dat$tworisc, x=statemeanDLHS_AHS.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanDLHS_AHS.dat$threerisc, x=statemeanDLHS_AHS.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemeanDLHS_AHS.dat$fourrisc, x=statemeanDLHS_AHS.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDLHS_AHS.dat$onerisc ~ statemeanDLHS_AHS.dat$age,span=1)),x=statemeanDLHS_AHS.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDLHS_AHS.dat$tworisc ~ statemeanDLHS_AHS.dat$age,span=1)),x=statemeanDLHS_AHS.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDLHS_AHS.dat$threerisc ~ statemeanDLHS_AHS.dat$age,span=1)),x=statemeanDLHS_AHS.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDLHS_AHS.dat$fourrisc ~ statemeanDLHS_AHS.dat$age,span=1)),x=statemeanDLHS_AHS.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 risc factor"="firebrick1", ">=2 risc factor"="firebrick3", 
                                 ">=3 risc factor"="firebrick4", ">=4 risc factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(18, 28, 38 ,49), limits=c(18, 49)) +
  coord_fixed(31/100, expand=F)
stateawarefig








DHS <- filter(merg, svy=="DHS" )


DHS <- filter(DHS, age>=18 & age<50)


#####dummy variables for each number of risc factors

DHS <- mutate(DHS, 
                    zero_risc = ifelse(sum_CVD_risc_dbl==0,1,0),
                    one_risc = ifelse(sum_CVD_risc_dbl>=1,1,0),
                     two_risc = ifelse(sum_CVD_risc_dbl>=2,1,0),
                     three_risc = ifelse(sum_CVD_risc_dbl>=3,1,0),
                     four_risc = ifelse(sum_CVD_risc_dbl>=4,1,0),
                     five_risc = ifelse(sum_CVD_risc_dbl>=5,1,0),
                     six_risc = ifelse(sum_CVD_risc_dbl>=6,1,0),
                     seven_risc = ifelse(sum_CVD_risc_dbl>=7,1,0),
                     eight_risc = ifelse(sum_CVD_risc_dbl>=8,1,0))
               
               


library(spatstat)
statemeanDHS.dat <- DHS %>%
  group_by(age) %>%
  mutate(zerorisc = weighted.mean(zero_risc, sworld_weight_india, na.rm=TRUE)*100,
         onerisc = weighted.mean(one_risc, sworld_weight_india, na.rm=TRUE)*100, 
         tworisc = weighted.mean(two_risc, sworld_weight_india, na.rm=TRUE)*100,
         threerisc = weighted.mean(three_risc, sworld_weight_india, na.rm=TRUE)*100,
         fourrisc = weighted.mean(four_risc, sworld_weight_india, na.rm=TRUE)*100,
         fiverisc = weighted.mean(five_risc, sworld_weight_india, na.rm=TRUE)*100,
         sixrisc =weighted.mean(six_risc, sworld_weight_india, na.rm=TRUE)*100,
         sevenrisc = weighted.mean(seven_risc, sworld_weight_india, na.rm=TRUE)*100,
         eightrisc = weighted.mean(eight_risc, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zerorisc, onerisc, tworisc, threerisc, fourrisc, fiverisc, sixrisc, sevenrisc, eightrisc)



#####Aware


stateawarefig <- statemeanDHS.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemeanDHS.dat$onerisc, x=statemeanDHS.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanDHS.dat$tworisc, x=statemeanDHS.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemeanDHS.dat$threerisc, x=statemeanDHS.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemeanDHS.dat$fourrisc, x=statemeanDHS.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDHS.dat$onerisc ~ statemeanDHS.dat$age,span=1)),x=statemeanDHS.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDHS.dat$tworisc ~ statemeanDHS.dat$age,span=1)),x=statemeanDHS.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDHS.dat$threerisc ~ statemeanDHS.dat$age,span=1)),x=statemeanDHS.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanDHS.dat$fourrisc ~ statemeanDHS.dat$age,span=1)),x=statemeanDHS.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 risc factor"="firebrick1", ">=2 risc factor"="firebrick3", 
                                 ">=3 risc factor"="firebrick4", ">=4 risc factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
   scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(18, 28, 38 ,49), limits=c(18, 49)) +
  coord_fixed(31/100, expand=F)
stateawarefig







```




```{r multivariable regressions CVD morb!!!!}

install.packages("DataCombine")
library(DataCombine)


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))



#####regression

###make urban rural dataset

merg_urban <- filter(merg, urban==1)
merg_rural <- filter(merg, urban==0)


####URBAN

## cvdmorb

CVDmorb <- glm.cluster(formula = multi_CVD_morb_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth + smoke + csmkls_tb,  cluster="psu", data=merg_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDmorb), confint(CVDmorb)))
write.csv(aware, "RR cvdmorb poisson urban.csv")

results_aware <-summary(CVDmorb)
write.csv(results_aware, "p Value cvdmorb poisson urban.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdmorb poisson urban.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdmorb poisson urban.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdmorb"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)



 write.csv(joint, "cvdmorb poisson urban.csv")
 

 ####Rural

## cvdmorb

CVDmorb <- glm.cluster(formula = multi_CVD_morb_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth + smoke + csmkls_tb,  cluster="psu", data=merg_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDmorb), confint(CVDmorb)))
write.csv(aware, "RR cvdmorb poisson rural.csv")

results_aware <-summary(CVDmorb)
write.csv(results_aware, "p Value cvdmorb poisson rural.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdmorb poisson rural.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdmorb poisson rural.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdmorb"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "cvdmorb poisson rural.csv")
 

 
 
 
```

```{r multivariable regressions CVD risc!!!! separate for dlhs/ahs and DHS}

install.packages("DataCombine")
library(DataCombine)


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))




################## zone_news as per: https://en.wikipedia.org/wiki/Administrative_divisions_of_India
merg <- mutate(merg, 
                  # Nothern
                  zone_new = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir", "North",
                                          # Northeastern
                                          ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                 # Central
                                                 ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                        # Eastern
                                                        ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                               # Western
                                                               ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                      # Southern
                                                                      ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))



DLHS_AHS <- filter(merg, svy=="AHS" | svy=="DLHS")


DLHS_AHS <- filter(DLHS_AHS, age>=18 & age<50)


#####regression

###make urban rural dataset

DLHS_AHS_urban <- filter(DLHS_AHS, urban==1)
DLHS_AHS_rural <- filter(DLHS_AHS, urban==0)


####URBAN

## cvdrisc

CVDrisc <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDrisc), confint(CVDrisc)))
write.csv(aware, "RR cvdrisc poisson urban DLHS_AHS only.csv")

results_aware <-summary(CVDrisc)
write.csv(results_aware, "p Value cvdrisc poisson urban DLHS_AHS only.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdrisc poisson urban DLHS_AHS only.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdrisc poisson urban DLHS_AHS only.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)



 write.csv(joint, "cvdrisc poisson urban DLHS_AHS only.csv")
 

 ####Rural

## cvdrisc

CVDrisc <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDrisc), confint(CVDrisc)))
write.csv(aware, "RR cvdrisc poisson rural DLHS_AHS only.csv")

results_aware <-summary(CVDrisc)
write.csv(results_aware, "p Value cvdrisc poisson rural DLHS_AHS only.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdrisc poisson rural DLHS_AHS only.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdrisc poisson rural DLHS_AHS only.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "cvdrisc poisson rural DLHS_AHS only.csv")
 

 
 DHS <- filter(merg, svy=="DHS" )

DHS <- filter(DHS, age>=18 & age<50)


#####regression

###make urban rural dataset

DHS_urban <- filter(DHS, urban==1)
DHS_rural <- filter(DHS, urban==0)


####URBAN

## cvdrisc

CVDrisc <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=DHS_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDrisc), confint(CVDrisc)))
write.csv(aware, "RR cvdrisc poisson urban DHS only.csv")

results_aware <-summary(CVDrisc)
write.csv(results_aware, "p Value cvdrisc poisson urban DHS only.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdrisc poisson urban DHS only.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdrisc poisson urban DHS only.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)



 write.csv(joint, "cvdrisc poisson urban DHS only.csv")
 

 ####Rural

## cvdrisc

CVDrisc <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=DHS_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDrisc), confint(CVDrisc)))
write.csv(aware, "RR cvdrisc poisson rural DHS only.csv")

results_aware <-summary(CVDrisc)
write.csv(results_aware, "p Value cvdrisc poisson rural DHS only.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdrisc poisson rural DHS only.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdrisc poisson rural DHS only.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "cvdrisc poisson rural DHS only.csv")
 



 
```



```{r univariable regressions CVD risc}


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))




################## zone_news as per: https://en.wikipedia.org/wiki/Administrative_divisions_of_India
merg <- mutate(merg, 
                  # Nothern
                  zone_new = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir", "North",
                                          # Northeastern
                                          ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                 # Central
                                                 ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                        # Eastern
                                                        ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                               # Western
                                                               ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                      # Southern
                                                                      ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))




###make urban rural dataset

merg_urban <- filter(merg, urban==1)
merg_rural <- filter(merg, urban==0)



###univariable regressions CVD risc URBAN     ###



age <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results CVD risc univariable reg urban.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value CVD risc univariable reg urban.csv")

wealth <- glm.cluster(formula = multi_CVD_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results CVD risc  univariable reg urban.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value CVD risc univariable reg urban.csv")

educat <- glm.cluster(formula = multi_CVD_risc_dbl ~  educatnames ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results CVD risc univariable reg urban.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value CVD risc univariable reg urban.csv")

married <- glm.cluster(formula = multi_CVD_risc_dbl ~  married ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results CVD risc univariable reg urban.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value CVD risc univariable reg urban.csv")

sex <- glm.cluster(formula = multi_CVD_risc_dbl ~  sex ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results CVD risc univariable reg urban.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value CVD risc univariable reg urban.csv")

zone_new <- glm.cluster(formula = multi_CVD_risc_dbl ~ zone_new,  cluster="psu", data=merg_urban, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results CVD risc univariable reg urban.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value CVD risc univariable reg urban.csv")


districtwealth <- glm.cluster(formula = multi_CVD_risc_dbl ~ district_medianwealth ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results CVD risc univariable reg urban.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value CVD risc univariable reg urban.csv")




##univariable regressions CVD risc rural     #



age <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results CVD risc univariable reg rural.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value CVD risc univariable reg rural.csv")

wealth <- glm.cluster(formula = multi_CVD_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results CVD risc  univariable reg rural.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value CVD risc univariable reg rural.csv")

educat <- glm.cluster(formula = multi_CVD_risc_dbl ~  educatnames ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results CVD risc univariable reg rural.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value CVD risc univariable reg rural.csv")

married <- glm.cluster(formula = multi_CVD_risc_dbl ~  married ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results CVD risc univariable reg rural.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value CVD risc univariable reg rural.csv")

sex <- glm.cluster(formula = multi_CVD_risc_dbl ~  sex ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results CVD risc univariable reg rural.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value CVD risc univariable reg rural.csv")

zone_new <- glm.cluster(formula = multi_CVD_risc_dbl ~ zone_new,  cluster="psu", data=merg_rural, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results CVD risc univariable reg rural.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value CVD risc univariable reg rural.csv")


districtwealth <- glm.cluster(formula = multi_CVD_risc_dbl ~ district_medianwealth ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results CVD risc univariable reg rural.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value CVD risc univariable reg rural.csv")






```





```{r univariable regressions CVD risc separately for dlhs/ahs and dhs }


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))




################## zone_news as per: https://en.wikipedia.org/wiki/Administrative_divisions_of_India
merg <- mutate(merg, 
                  # Nothern
                  zone_new = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir", "North",
                                          # Northeastern
                                          ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                 # Central
                                                 ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                        # Eastern
                                                        ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                               # Western
                                                               ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                      # Southern
                                                                      ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))


DLHS_AHS <- filter(merg, svy=="AHS" | svy=="DLHS")


DLHS_AHS <- filter(DLHS_AHS, age>=18 & age<50)



###make urban rural dataset

DLHS_AHS_urban <- filter(DLHS_AHS, urban==1)
DLHS_AHS_rural <- filter(DLHS_AHS, urban==0)



###univariable regressions CVD risc URBAN     ###



age <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results CVD risc univariable reg urban DLHS_AHS only.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value CVD risc univariable reg urban DLHS_AHS only.csv")

wealth <- glm.cluster(formula = multi_CVD_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results CVD risc  univariable reg urban DLHS_AHS only.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value CVD risc univariable reg urban DLHS_AHS only.csv")

educat <- glm.cluster(formula = multi_CVD_risc_dbl ~  educatnames ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results CVD risc univariable reg urban DLHS_AHS only.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value CVD risc univariable reg urban DLHS_AHS only.csv")

married <- glm.cluster(formula = multi_CVD_risc_dbl ~  married ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results CVD risc univariable reg urban DLHS_AHS only.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value CVD risc univariable reg urban DLHS_AHS only.csv")

sex <- glm.cluster(formula = multi_CVD_risc_dbl ~  sex ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results CVD risc univariable reg urban DLHS_AHS only.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value CVD risc univariable reg urban DLHS_AHS only.csv")

zone_new <- glm.cluster(formula = multi_CVD_risc_dbl ~ zone_new,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results CVD risc univariable reg urban DLHS_AHS only.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value CVD risc univariable reg urban DLHS_AHS only.csv")


districtwealth <- glm.cluster(formula = multi_CVD_risc_dbl ~ district_medianwealth ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results CVD risc univariable reg urban DLHS_AHS only.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value CVD risc univariable reg urban DLHS_AHS only.csv")




##univariable regressions CVD risc rural     #



age <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results CVD risc univariable reg rural DLHS_AHS only.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value CVD risc univariable reg rural DLHS_AHS only.csv")

wealth <- glm.cluster(formula = multi_CVD_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results CVD risc  univariable reg rural DLHS_AHS only.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value CVD risc univariable reg rural DLHS_AHS only.csv")

educat <- glm.cluster(formula = multi_CVD_risc_dbl ~  educatnames ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results CVD risc univariable reg rural DLHS_AHS only.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value CVD risc univariable reg rural DLHS_AHS only.csv")

married <- glm.cluster(formula = multi_CVD_risc_dbl ~  married ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results CVD risc univariable reg rural DLHS_AHS only.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value CVD risc univariable reg rural DLHS_AHS only.csv")

sex <- glm.cluster(formula = multi_CVD_risc_dbl ~  sex ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results CVD risc univariable reg rural DLHS_AHS only.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value CVD risc univariable reg rural DLHS_AHS only.csv")

zone_new <- glm.cluster(formula = multi_CVD_risc_dbl ~ zone_new,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results CVD risc univariable reg rural DLHS_AHS only.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value CVD risc univariable reg rural DLHS_AHS only.csv")


districtwealth <- glm.cluster(formula = multi_CVD_risc_dbl ~ district_medianwealth ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results CVD risc univariable reg rural DLHS_AHS only.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value CVD risc univariable reg rural DLHS_AHS only.csv")



 DHS <- filter(merg, svy=="DHS" )

DHS <- filter(DHS, age>=18 & age<50)




###make urban rural dataset

DHS_urban <- filter(DHS, urban==1)
DHS_rural <- filter(DHS, urban==0)



###univariable regressions CVD risc URBAN     ###



age <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results CVD risc univariable reg urban DHS only.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value CVD risc univariable reg urban DHS only.csv")

wealth <- glm.cluster(formula = multi_CVD_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results CVD risc  univariable reg urban DHS only.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value CVD risc univariable reg urban DHS only.csv")

educat <- glm.cluster(formula = multi_CVD_risc_dbl ~  educatnames ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results CVD risc univariable reg urban DHS only.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value CVD risc univariable reg urban DHS only.csv")

married <- glm.cluster(formula = multi_CVD_risc_dbl ~  married ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results CVD risc univariable reg urban DHS only.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value CVD risc univariable reg urban DHS only.csv")

sex <- glm.cluster(formula = multi_CVD_risc_dbl ~  sex ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results CVD risc univariable reg urban DHS only.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value CVD risc univariable reg urban DHS only.csv")

zone_new <- glm.cluster(formula = multi_CVD_risc_dbl ~ zone_new,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results CVD risc univariable reg urban DHS only.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value CVD risc univariable reg urban DHS only.csv")


districtwealth <- glm.cluster(formula = multi_CVD_risc_dbl ~ district_medianwealth ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results CVD risc univariable reg urban DHS only.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value CVD risc univariable reg urban DHS only.csv")




##univariable regressions CVD risc rural     #



age <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results CVD risc univariable reg rural DHS only.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value CVD risc univariable reg rural DHS only.csv")

wealth <- glm.cluster(formula = multi_CVD_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results CVD risc  univariable reg rural DHS only.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value CVD risc univariable reg rural DHS only.csv")

educat <- glm.cluster(formula = multi_CVD_risc_dbl ~  educatnames ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results CVD risc univariable reg rural DHS only.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value CVD risc univariable reg rural DHS only.csv")

married <- glm.cluster(formula = multi_CVD_risc_dbl ~  married ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results CVD risc univariable reg rural DHS only.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value CVD risc univariable reg rural DHS only.csv")

sex <- glm.cluster(formula = multi_CVD_risc_dbl ~  sex ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results CVD risc univariable reg rural DHS only.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value CVD risc univariable reg rural DHS only.csv")

zone_new <- glm.cluster(formula = multi_CVD_risc_dbl ~ zone_new,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results CVD risc univariable reg rural DHS only.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value CVD risc univariable reg rural DHS only.csv")


districtwealth <- glm.cluster(formula = multi_CVD_risc_dbl ~ district_medianwealth ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results CVD risc univariable reg rural DHS only.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value CVD risc univariable reg rural DHS only.csv")







```




```{r multivariable regressions ALL morb!!!!}

install.packages("DataCombine")
library(DataCombine)


dhs_nomiss$wealth_quintile_rurb <- as.numeric(dhs_nomiss$wealth_quintile_rurb)

        dhs_nomiss <- dhs_nomiss %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        dhs_nomiss <- dhs_nomiss %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))




#####regression

###make urban rural dataset

dhs_nomiss_urban <- filter(dhs_nomiss, urban==1)
dhs_nomiss_rural <- filter(dhs_nomiss, urban==0)


####URBAN

## ALLmorb

ALLmorb <- glm.cluster(formula = multi_morbid_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth + tobacco_smoked + tobacco_smokeless,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(ALLmorb), confint(ALLmorb)))
write.csv(aware, "RR ALLmorb poisson urban.csv")

results_aware <-summary(ALLmorb)
write.csv(results_aware, "p Value ALLmorb poisson urban.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR ALLmorb poisson urban.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value ALLmorb poisson urban.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 ALLmorb"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)



 write.csv(joint, "ALLmorb poisson urban.csv")
 

 ####Rural

## ALLmorb

ALLmorb <- glm.cluster(formula = multi_morbid_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth  + tobacco_smoked + tobacco_smokeless,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(ALLmorb), confint(ALLmorb)))
write.csv(aware, "RR ALLmorb poisson rural.csv")

results_aware <-summary(ALLmorb)
write.csv(results_aware, "p Value ALLmorb poisson rural.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR ALLmorb poisson rural.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value ALLmorb poisson rural.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 ALLmorb"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "ALLmorb poisson rural.csv")
 

 
 
 
```





```{r multivariable regressions and age group prevalence ALL risc and CVD risc ASSUMING ALl NOT FASTED IN AHS!!!!}

install.packages("DataCombine")
library(DataCombine)


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))




################## zone_news as per: https://en.wikipedia.org/wiki/Administrative_divisions_of_India
merg <- mutate(merg, 
                  # Nothern
                  zone_new = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir", "North",
                                          # Northeastern
                                          ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                 # Central
                                                 ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                        # Eastern
                                                        ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                               # Western
                                                               ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                      # Southern
                                                                      ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))



merg <- mutate(merg,
              ex_diab_narrow_ind_AHS = ifelse( is.na(ex_glucose_ind)==T, NA, 
                                                  ifelse(svy=="AHS" & ex_glucose_ind>=200, 1, 0)))


AHS <- mutate(AHS,
              ex_diab_narrow_ind_AHS = ifelse( is.na(ex_glucose_ind)==T, NA, 
                                           ifelse(ex_glucose_ind>=200, 1,0)))

AHS$ex_diab_narrow_ind_AHS <- as.factor (AHS$ex_diab_narrow_ind_AHS)
merg$ex_diab_narrow_ind_AHS <- as.factor (merg$ex_diab_narrow_ind_AHS)

merg <- mutate(merg,
              ex_diab_narrow_ind_AHS_nonfasted = ifelse(svy=="AHS" & ex_diab_narrow_ind_AHS==1, 1,
                                                      ifelse(svy=="DLHS" & ex_diab_narrow_ind==1,1,
                                                             ifelse(svy=="DHS" & ex_diab_narrow_ind==1,1,0))))
merg$ex_diab_narrow_ind_AHS_nonfasted_dbl <- as.numeric(merg$ex_diab_narrow_ind_AHS_nonfasted)
merg$ex_diab_narrow_ind_AHS_nonfasted <- as.factor(merg$ex_diab_narrow_ind_AHS_nonfasted)

#####make risc_unfasted factor dummy

merg<- mutate(merg, 
              sum_risc_unfasted = obese_dbl + sev_underweight_dbl + ex_diab_narrow_ind_AHS_nonfasted_dbl + ex_htn_narrow_ind_dbl+ smoke_dbl + csmkls_tb_dbl)     
merg <- mutate(merg, 
               sum_risc_unfasted_dbl = as.numeric(sum_risc_unfasted))

merg <- mutate(merg,
               multi_risc_unfasted = ifelse(sum_risc_unfasted>=2,1,0))

merg <- mutate(merg, 
               multi_risc_unfasted_dbl = as.numeric(multi_risc_unfasted))

merg$multi_risc_unfasted <- as.factor(merg$multi_risc_unfasted)
merg$sum_risc_unfasted <- as.factor(merg$sum_risc_unfasted)

summary(merg$multi_risc_unfasted)
summary(merg$sum_risc_unfasted)


summary(merg$obese)
summary(merg$sev_underweight)
summary(merg$ex_diab_narrow_ind)
summary(merg$ex_htn_narrow_ind)
summary(merg$smoke)
summary(merg$csmkls_tb)




#####make CVD risc_unfasted factor dummy

merg<- mutate(merg, 
              sum_CVD_risc_unfasted = obese_dbl  + ex_diab_narrow_ind_AHS_nonfasted_dbl + ex_htn_narrow_ind_dbl+ smoke_dbl )     
merg <- mutate(merg, 
               sum_CVD_risc_unfasted_dbl = as.numeric(sum_CVD_risc_unfasted))

merg <- mutate(merg,
               multi_CVD_risc_unfasted = ifelse(sum_CVD_risc_unfasted>=2,1,0))

merg <- mutate(merg, 
               multi_CVD_risc_unfasted_dbl = as.numeric(multi_CVD_risc_unfasted))

merg$multi_CVD_risc_unfasted <- as.factor(merg$multi_CVD_risc_unfasted)
merg$sum_CVD_risc_unfasted <- as.factor(merg$sum_CVD_risc_unfasted)

summary(merg$multi_CVD_risc_unfasted)
summary(merg$sum_CVD_risc_unfasted)


summary(merg$obese)
summary(merg$sev_underweight)
summary(merg$ex_diab_narrow_ind)
summary(merg$ex_htn_narrow_ind)
summary(merg$smoke)
summary(merg$csmkls_tb)





#####regression

###make urban rural dataset

merg_urban <- filter(merg, urban==1)
merg_rural <- filter(merg, urban==0)


####URBAN

## ALLrisc_unfasted

ALLrisc_unfasted <- glm.cluster(formula = multi_risc_unfasted_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=merg_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(ALLrisc_unfasted), confint(ALLrisc_unfasted)))
write.csv(aware, "RR ALLrisc_unfasted poisson urban.csv")

results_aware <-summary(ALLrisc_unfasted)
write.csv(results_aware, "p Value ALLrisc_unfasted poisson urban.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR ALLrisc_unfasted poisson urban.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value ALLrisc_unfasted poisson urban.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 ALLrisc_unfasted"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)



 write.csv(joint, "ALLrisc_unfasted poisson urban.csv")
 

 ####Rural

## ALLrisc_unfasted

ALLrisc_unfasted <- glm.cluster(formula = multi_risc_unfasted_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=merg_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(ALLrisc_unfasted), confint(ALLrisc_unfasted)))
write.csv(aware, "RR ALLrisc_unfasted poisson rural.csv")

results_aware <-summary(ALLrisc_unfasted)
write.csv(results_aware, "p Value ALLrisc_unfasted poisson rural.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR ALLrisc_unfasted poisson rural.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value ALLrisc_unfasted poisson rural.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 ALLrisc_unfasted"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "ALLrisc_unfasted poisson rural.csv")
 

 ####URBAN

## cvdrisc_unfasted

CVDrisc_unfasted <- glm.cluster(formula = multi_CVD_risc_unfasted_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=merg_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDrisc_unfasted), confint(CVDrisc_unfasted)))
write.csv(aware, "RR cvdrisc_unfasted poisson urban.csv")

results_aware <-summary(CVDrisc_unfasted)
write.csv(results_aware, "p Value cvdrisc_unfasted poisson urban.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdrisc_unfasted poisson urban.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdrisc_unfasted poisson urban.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc_unfasted"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)



 write.csv(joint, "cvdrisc_unfasted poisson urban.csv")
 

 ####Rural

## cvdrisc_unfasted

CVDrisc_unfasted <- glm.cluster(formula = multi_CVD_risc_unfasted_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=merg_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDrisc_unfasted), confint(CVDrisc_unfasted)))
write.csv(aware, "RR cvdrisc_unfasted poisson rural.csv")

results_aware <-summary(CVDrisc_unfasted)
write.csv(results_aware, "p Value cvdrisc_unfasted poisson rural.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdrisc_unfasted poisson rural.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdrisc_unfasted poisson rural.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc_unfasted"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "cvdrisc_unfasted poisson rural.csv")
 

 
 
 

merg <- mutate(merg, 
                    zero_risc_unfasted = ifelse(sum_risc_unfasted_dbl==0,1,0),
                    one_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=1,1,0),
                     two_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=2,1,0),
                     three_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=3,1,0),
                     four_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=4,1,0),
                     five_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=5,1,0),
                     six_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=6,1,0),
                     seven_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=7,1,0),
                     eight_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc_unfasted, one_risc_unfasted, two_risc_unfasted, three_risc_unfasted, four_risc_unfasted, five_risc_unfasted, six_risc_unfasted))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_unfasted_pop = survey_mean(zero_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              one_risc_unfasted_pop = survey_mean(one_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              two_risc_unfasted_pop = survey_mean(two_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              three_risc_unfasted_pop = survey_mean(three_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              four_risc_unfasted_pop = survey_mean(four_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              five_risc_unfasted_pop = survey_mean(five_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              six_risc_unfasted_pop = survey_mean(six_risc_unfasted, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtot, "10 year age group prevalence multirisc_unfasted.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.multirisc_unfasted` <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence multirisc_unfasted.csv")
aware <- `10.year.age.group.prevalence.multirisc_unfasted`



aware <- mutate(aware,
                zero_risc_unfasted_pop = round(zero_risc_unfasted_pop,2),
                one_risc_unfasted_pop = round(one_risc_unfasted_pop,2),
                two_risc_unfasted_pop = round(two_risc_unfasted_pop,2),
                three_risc_unfasted_pop = round(three_risc_unfasted_pop,2),
                four_risc_unfasted_pop = round(four_risc_unfasted_pop,2),
                five_risc_unfasted_pop = round(five_risc_unfasted_pop,2),
                six_risc_unfasted_pop = round(six_risc_unfasted_pop,2),
                zero_risc_unfasted_pop_low = round(zero_risc_unfasted_pop_low,2),
                one_risc_unfasted_pop_low = round(one_risc_unfasted_pop_low,2),
                two_risc_unfasted_pop_low = round(two_risc_unfasted_pop_low,2),
                three_risc_unfasted_pop_low = round(three_risc_unfasted_pop_low,2),
                four_risc_unfasted_pop_low = round(four_risc_unfasted_pop_low,2),
                five_risc_unfasted_pop_low = round(five_risc_unfasted_pop_low,2),
                six_risc_unfasted_pop_low = round(six_risc_unfasted_pop_low,2),
                zero_risc_unfasted_pop_upp = round(zero_risc_unfasted_pop_upp,2),
                one_risc_unfasted_pop_upp = round(one_risc_unfasted_pop_upp,2),
                two_risc_unfasted_pop_upp = round(two_risc_unfasted_pop_upp,2),
                three_risc_unfasted_pop_upp = round(three_risc_unfasted_pop_upp,2),
                four_risc_unfasted_pop_upp = round(four_risc_unfasted_pop_upp,2),
                five_risc_unfasted_pop_upp = round(five_risc_unfasted_pop_upp,2),
                six_risc_unfasted_pop_upp = round(six_risc_unfasted_pop_upp,2))


aware$zero_risc_unfasted_pop <- sprintf("%.2f", aware$zero_risc_unfasted_pop)
aware$one_risc_unfasted_pop <- sprintf("%.2f", aware$one_risc_unfasted_pop)
aware$two_risc_unfasted_pop <- sprintf("%.2f", aware$two_risc_unfasted_pop)
aware$three_risc_unfasted_pop <- sprintf("%.2f", aware$three_risc_unfasted_pop)
aware$four_risc_unfasted_pop <- sprintf("%.2f", aware$four_risc_unfasted_pop)
aware$five_risc_unfasted_pop <- sprintf("%.2f", aware$five_risc_unfasted_pop)
aware$six_risc_unfasted_pop <- sprintf("%.2f", aware$six_risc_unfasted_pop)
aware$zero_risc_unfasted_pop_low <- sprintf("%.2f", aware$zero_risc_unfasted_pop_low)
aware$one_risc_unfasted_pop_low <- sprintf("%.2f", aware$one_risc_unfasted_pop_low)
aware$two_risc_unfasted_pop_low <- sprintf("%.2f", aware$two_risc_unfasted_pop_low)
aware$three_risc_unfasted_pop_low <- sprintf("%.2f", aware$three_risc_unfasted_pop_low)
aware$four_risc_unfasted_pop_low <- sprintf("%.2f", aware$four_risc_unfasted_pop_low)
aware$five_risc_unfasted_pop_low <- sprintf("%.2f", aware$five_risc_unfasted_pop_low)
aware$six_risc_unfasted_pop_low <- sprintf("%.2f", aware$six_risc_unfasted_pop_low)
aware$zero_risc_unfasted_pop_upp <- sprintf("%.2f", aware$zero_risc_unfasted_pop_upp)
aware$one_risc_unfasted_pop_upp <- sprintf("%.2f", aware$one_risc_unfasted_pop_upp)
aware$two_risc_unfasted_pop_upp <- sprintf("%.2f", aware$two_risc_unfasted_pop_upp)
aware$three_risc_unfasted_pop_upp <- sprintf("%.2f", aware$three_risc_unfasted_pop_upp)
aware$four_risc_unfasted_pop_upp <- sprintf("%.2f", aware$four_risc_unfasted_pop_upp)
aware$five_risc_unfasted_pop_upp <- sprintf("%.2f", aware$five_risc_unfasted_pop_upp)
aware$six_risc_unfasted_pop_upp <- sprintf("%.2f", aware$six_risc_unfasted_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_unfasted_pop_low, zero_risc_unfasted_pop_upp, sep="-"),
                citempone = str_c(one_risc_unfasted_pop_low, one_risc_unfasted_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_unfasted_pop_low, two_risc_unfasted_pop_upp, sep="-"),
                citempthree = str_c(three_risc_unfasted_pop_low, three_risc_unfasted_pop_upp, sep="-"),
                citempfour = str_c(four_risc_unfasted_pop_low, four_risc_unfasted_pop_upp, sep="-"),
                citempfive = str_c(five_risc_unfasted_pop_low, five_risc_unfasted_pop_upp, sep="-"),
                citempsix = str_c(six_risc_unfasted_pop_low, six_risc_unfasted_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_unfasted_pop, cizero, sep=" "),
                rrone = str_c(one_risc_unfasted_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_unfasted_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_unfasted_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_unfasted_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_unfasted_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_unfasted_pop, cisix, sep=" "))




write.csv(aware, "10 year prev risc_unfasted.csv")
 
 




merg <- mutate(merg, 
                    zero_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl==0,1,0),
                    one_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=1,1,0),
                     two_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=2,1,0),
                     three_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=3,1,0),
                     four_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=4,1,0),
                     five_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=5,1,0),
                     six_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=6,1,0),
                     seven_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=7,1,0),
                     eight_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=8,1,0))

 svy_ex_htn_narrow_ind <- merg %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp, zero_risc_unfasted, one_risc_unfasted, two_risc_unfasted, three_risc_unfasted, four_risc_unfasted, five_risc_unfasted, six_risc_unfasted))
  
  prevtot <- svy_ex_htn_narrow_ind %>%
    group_by(age_grp)%>%
    summarize(zero_risc_unfasted_pop = survey_mean(zero_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              one_risc_unfasted_pop = survey_mean(one_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              two_risc_unfasted_pop = survey_mean(two_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              three_risc_unfasted_pop = survey_mean(three_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              four_risc_unfasted_pop = survey_mean(four_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              five_risc_unfasted_pop = survey_mean(five_risc_unfasted, proportion=TRUE, vartype = "ci")*100,
              six_risc_unfasted_pop = survey_mean(six_risc_unfasted, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtot, "10 year age group prevalence multirisc_unfasted.csv")


  
  
  ######FORMATTING

`10.year.age.group.prevalence.multirisc_unfasted` <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/10 year age group prevalence multirisc_unfasted.csv")
aware <- `10.year.age.group.prevalence.multirisc_unfasted`



aware <- mutate(aware,
                zero_risc_unfasted_pop = round(zero_risc_unfasted_pop,2),
                one_risc_unfasted_pop = round(one_risc_unfasted_pop,2),
                two_risc_unfasted_pop = round(two_risc_unfasted_pop,2),
                three_risc_unfasted_pop = round(three_risc_unfasted_pop,2),
                four_risc_unfasted_pop = round(four_risc_unfasted_pop,2),
                five_risc_unfasted_pop = round(five_risc_unfasted_pop,2),
                six_risc_unfasted_pop = round(six_risc_unfasted_pop,2),
                zero_risc_unfasted_pop_low = round(zero_risc_unfasted_pop_low,2),
                one_risc_unfasted_pop_low = round(one_risc_unfasted_pop_low,2),
                two_risc_unfasted_pop_low = round(two_risc_unfasted_pop_low,2),
                three_risc_unfasted_pop_low = round(three_risc_unfasted_pop_low,2),
                four_risc_unfasted_pop_low = round(four_risc_unfasted_pop_low,2),
                five_risc_unfasted_pop_low = round(five_risc_unfasted_pop_low,2),
                six_risc_unfasted_pop_low = round(six_risc_unfasted_pop_low,2),
                zero_risc_unfasted_pop_upp = round(zero_risc_unfasted_pop_upp,2),
                one_risc_unfasted_pop_upp = round(one_risc_unfasted_pop_upp,2),
                two_risc_unfasted_pop_upp = round(two_risc_unfasted_pop_upp,2),
                three_risc_unfasted_pop_upp = round(three_risc_unfasted_pop_upp,2),
                four_risc_unfasted_pop_upp = round(four_risc_unfasted_pop_upp,2),
                five_risc_unfasted_pop_upp = round(five_risc_unfasted_pop_upp,2),
                six_risc_unfasted_pop_upp = round(six_risc_unfasted_pop_upp,2))


aware$zero_risc_unfasted_pop <- sprintf("%.2f", aware$zero_risc_unfasted_pop)
aware$one_risc_unfasted_pop <- sprintf("%.2f", aware$one_risc_unfasted_pop)
aware$two_risc_unfasted_pop <- sprintf("%.2f", aware$two_risc_unfasted_pop)
aware$three_risc_unfasted_pop <- sprintf("%.2f", aware$three_risc_unfasted_pop)
aware$four_risc_unfasted_pop <- sprintf("%.2f", aware$four_risc_unfasted_pop)
aware$five_risc_unfasted_pop <- sprintf("%.2f", aware$five_risc_unfasted_pop)
aware$six_risc_unfasted_pop <- sprintf("%.2f", aware$six_risc_unfasted_pop)
aware$zero_risc_unfasted_pop_low <- sprintf("%.2f", aware$zero_risc_unfasted_pop_low)
aware$one_risc_unfasted_pop_low <- sprintf("%.2f", aware$one_risc_unfasted_pop_low)
aware$two_risc_unfasted_pop_low <- sprintf("%.2f", aware$two_risc_unfasted_pop_low)
aware$three_risc_unfasted_pop_low <- sprintf("%.2f", aware$three_risc_unfasted_pop_low)
aware$four_risc_unfasted_pop_low <- sprintf("%.2f", aware$four_risc_unfasted_pop_low)
aware$five_risc_unfasted_pop_low <- sprintf("%.2f", aware$five_risc_unfasted_pop_low)
aware$six_risc_unfasted_pop_low <- sprintf("%.2f", aware$six_risc_unfasted_pop_low)
aware$zero_risc_unfasted_pop_upp <- sprintf("%.2f", aware$zero_risc_unfasted_pop_upp)
aware$one_risc_unfasted_pop_upp <- sprintf("%.2f", aware$one_risc_unfasted_pop_upp)
aware$two_risc_unfasted_pop_upp <- sprintf("%.2f", aware$two_risc_unfasted_pop_upp)
aware$three_risc_unfasted_pop_upp <- sprintf("%.2f", aware$three_risc_unfasted_pop_upp)
aware$four_risc_unfasted_pop_upp <- sprintf("%.2f", aware$four_risc_unfasted_pop_upp)
aware$five_risc_unfasted_pop_upp <- sprintf("%.2f", aware$five_risc_unfasted_pop_upp)
aware$six_risc_unfasted_pop_upp <- sprintf("%.2f", aware$six_risc_unfasted_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_risc_unfasted_pop_low, zero_risc_unfasted_pop_upp, sep="-"),
                citempone = str_c(one_risc_unfasted_pop_low, one_risc_unfasted_pop_upp, sep="-"),
                citemptwo = str_c(two_risc_unfasted_pop_low, two_risc_unfasted_pop_upp, sep="-"),
                citempthree = str_c(three_risc_unfasted_pop_low, three_risc_unfasted_pop_upp, sep="-"),
                citempfour = str_c(four_risc_unfasted_pop_low, four_risc_unfasted_pop_upp, sep="-"),
                citempfive = str_c(five_risc_unfasted_pop_low, five_risc_unfasted_pop_upp, sep="-"),
                citempsix = str_c(six_risc_unfasted_pop_low, six_risc_unfasted_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_risc_unfasted_pop, cizero, sep=" "),
                rrone = str_c(one_risc_unfasted_pop, cione, sep=" "),
                rrtwo = str_c(two_risc_unfasted_pop, citwo, sep=" "),
                rrthree = str_c(three_risc_unfasted_pop, cithree, sep=" "),
                rrfour = str_c(four_risc_unfasted_pop, cifour, sep=" "),
                rrfive = str_c(five_risc_unfasted_pop, cifive, sep=" "),
                rrsix = str_c(six_risc_unfasted_pop, cisix, sep=" "))




write.csv(aware, "10 year prev CVD risc_unfasted!!!!!!.csv")

 



#####dummy variables for each number of risc_unfasted factors

merg <- mutate(merg, 
                    zero_risc_unfasted = ifelse(sum_risc_unfasted_dbl==0,1,0),
                    one_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=1,1,0),
                     two_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=2,1,0),
                     three_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=3,1,0),
                     four_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=4,1,0),
                     five_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=5,1,0),
                     six_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=6,1,0),
                     seven_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=7,1,0),
                     eight_risc_unfasted = ifelse(sum_risc_unfasted_dbl>=8,1,0))
               
               

mergsmaller80 <- filter(merg, age<=80)

library(spatstat)
statemean.dat <- mergsmaller80 %>%
  group_by(age) %>%
  mutate(zerorisc_unfasted = weighted.mean(zero_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         onerisc_unfasted = weighted.mean(one_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100, 
         tworisc_unfasted = weighted.mean(two_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         threerisc_unfasted = weighted.mean(three_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         fourrisc_unfasted = weighted.mean(four_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         fiverisc_unfasted = weighted.mean(five_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         sixrisc_unfasted =weighted.mean(six_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         sevenrisc_unfasted = weighted.mean(seven_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         eightrisc_unfasted = weighted.mean(eight_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zerorisc_unfasted, onerisc_unfasted, tworisc_unfasted, threerisc_unfasted, fourrisc_unfasted, fiverisc_unfasted, sixrisc_unfasted, sevenrisc_unfasted, eightrisc_unfasted)



#####Aware


stateawarefig <- statemean.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemean.dat$onerisc_unfasted, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$tworisc_unfasted, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$threerisc_unfasted, x=statemean.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemean.dat$fourrisc_unfasted, x=statemean.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$onerisc_unfasted ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$tworisc_unfasted ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$threerisc_unfasted ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$fourrisc_unfasted ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 risc_unfasted factor"="firebrick1", ">=2 risc_unfasted factor"="firebrick3", 
                                 ">=3 risc_unfasted factor"="firebrick4", ">=4 risc_unfasted factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20, 40, 60,80), limits=c(15, 80)) +
  coord_fixed(65/100, expand=F)
stateawarefig


 

#####dummy variables for each number of risc_unfasted factors

merg <- mutate(merg, 
                    zero_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl==0,1,0),
                    one_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=1,1,0),
                     two_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=2,1,0),
                     three_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=3,1,0),
                     four_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=4,1,0),
                     five_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=5,1,0),
                     six_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=6,1,0),
                     seven_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=7,1,0),
                     eight_risc_unfasted = ifelse(sum_CVD_risc_unfasted_dbl>=8,1,0))
               
               

mergsmaller80 <- filter(merg, age<=80)

library(spatstat)
statemean.dat <- mergsmaller80 %>%
  group_by(age) %>%
  mutate(zerorisc_unfasted = weighted.mean(zero_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         onerisc_unfasted = weighted.mean(one_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100, 
         tworisc_unfasted = weighted.mean(two_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         threerisc_unfasted = weighted.mean(three_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         fourrisc_unfasted = weighted.mean(four_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         fiverisc_unfasted = weighted.mean(five_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         sixrisc_unfasted =weighted.mean(six_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         sevenrisc_unfasted = weighted.mean(seven_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         eightrisc_unfasted = weighted.mean(eight_risc_unfasted, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age,zerorisc_unfasted, onerisc_unfasted, tworisc_unfasted, threerisc_unfasted, fourrisc_unfasted, fiverisc_unfasted, sixrisc_unfasted, sevenrisc_unfasted, eightrisc_unfasted)



#####Aware


stateawarefig <- statemean.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemean.dat$onerisc_unfasted, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$tworisc_unfasted, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$threerisc_unfasted, x=statemean.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemean.dat$fourrisc_unfasted, x=statemean.dat$age),span=1, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$onerisc_unfasted ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$tworisc_unfasted ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$threerisc_unfasted ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$fourrisc_unfasted ~ statemean.dat$age,span=1)),x=statemean.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 risc_unfasted factor"="firebrick1", ">=2 risc_unfasted factor"="firebrick3", 
                                 ">=3 risc_unfasted factor"="firebrick4", ">=4 risc_unfasted factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20, 40, 60,80), limits=c(15, 80)) +
  coord_fixed(65/100, expand=F)
stateawarefig



 
```
 
 





```{r multivariable regressions ALL risc!!!! separately for dlhs/ahs and dhs}

install.packages("DataCombine")
library(DataCombine)


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))




################## zone_news as per: https://en.wikipedia.org/wiki/Administrative_divisions_of_India
merg <- mutate(merg, 
                  # Nothern
                  zone_new = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir", "North",
                                          # Northeastern
                                          ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                 # Central
                                                 ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                        # Eastern
                                                        ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                               # Western
                                                               ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                      # Southern
                                                                      ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))



DLHS_AHS <- filter(merg, svy=="AHS" | svy=="DLHS")


DLHS_AHS <- filter(DLHS_AHS, age>=18 & age<50)




#####regression

###make urban rural dataset

DLHS_AHS_urban <- filter(DLHS_AHS, urban==1)
DLHS_AHS_rural <- filter(DLHS_AHS, urban==0)


####URBAN

## ALLrisc

ALLrisc <- glm.cluster(formula = multi_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(ALLrisc), confint(ALLrisc)))
write.csv(aware, "RR ALLrisc poisson urban DLHS_AHS only.csv")

results_aware <-summary(ALLrisc)
write.csv(results_aware, "p Value ALLrisc poisson urban DLHS_AHS only.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR ALLrisc poisson urban DLHS_AHS only.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value ALLrisc poisson urban DLHS_AHS only.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 ALLrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)



 write.csv(joint, "ALLrisc poisson urban DLHS_AHS only.csv")
 

 ####Rural

## ALLrisc

ALLrisc <- glm.cluster(formula = multi_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(ALLrisc), confint(ALLrisc)))
write.csv(aware, "RR ALLrisc poisson rural DLHS_AHS only.csv")

results_aware <-summary(ALLrisc)
write.csv(results_aware, "p Value ALLrisc poisson rural DLHS_AHS only.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR ALLrisc poisson rural DLHS_AHS only.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value ALLrisc poisson rural DLHS_AHS only.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 ALLrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "ALLrisc poisson rural DLHS_AHS only.csv")
 
 DHS <- filter(merg, svy=="DHS" )

DHS <- filter(DHS, age>=18 & age<50)

 
 

#####regression

###make urban rural dataset

DHS_urban <- filter(DHS, urban==1)
DHS_rural <- filter(DHS, urban==0)


####URBAN

## ALLrisc

ALLrisc <- glm.cluster(formula = multi_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=DHS_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(ALLrisc), confint(ALLrisc)))
write.csv(aware, "RR ALLrisc poisson urban DHS only.csv")

results_aware <-summary(ALLrisc)
write.csv(results_aware, "p Value ALLrisc poisson urban DHS only.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR ALLrisc poisson urban DHS only.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value ALLrisc poisson urban DHS only.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 ALLrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)



 write.csv(joint, "ALLrisc poisson urban DHS only.csv")
 

 ####Rural

## ALLrisc

ALLrisc <- glm.cluster(formula = multi_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=DHS_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(ALLrisc), confint(ALLrisc)))
write.csv(aware, "RR ALLrisc poisson rural DHS only.csv")

results_aware <-summary(ALLrisc)
write.csv(results_aware, "p Value ALLrisc poisson rural DHS only.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR ALLrisc poisson rural DHS only.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value ALLrisc poisson rural DHS only.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 ALLrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "ALLrisc poisson rural DHS only.csv")
 
 
 
```



```{r univariable regressions ALL risc}


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))




################## zone_news as per: https://en.wikipedia.org/wiki/Administrative_divisions_of_India
merg <- mutate(merg, 
                  # Nothern
                  zone_new = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir", "North",
                                          # Northeastern
                                          ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                 # Central
                                                 ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                        # Eastern
                                                        ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                               # Western
                                                               ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                      # Southern
                                                                      ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))




###make urban rural dataset

merg_urban <- filter(merg, urban==1)
merg_rural <- filter(merg, urban==0)



#univariable regressions ALL risc URBAN     ##



age <- glm.cluster(formula = multi_risc_dbl ~ age_grp ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results ALL risc univariable reg urban.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value ALL risc univariable reg urban.csv")

wealth <- glm.cluster(formula = multi_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results ALL risc  univariable reg urban.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value ALL risc univariable reg urban.csv")

educat <- glm.cluster(formula = multi_risc_dbl ~  educatnames ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results ALL risc univariable reg urban.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value ALL risc univariable reg urban.csv")

married <- glm.cluster(formula = multi_risc_dbl ~  married ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results ALL risc univariable reg urban.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value ALL risc univariable reg urban.csv")

sex <- glm.cluster(formula = multi_risc_dbl ~  sex ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results ALL risc univariable reg urban.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value ALL risc univariable reg urban.csv")

zone_new <- glm.cluster(formula = multi_risc_dbl ~ zone_new,  cluster="psu", data=merg_urban, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results ALL risc univariable reg urban.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value ALL risc univariable reg urban.csv")


districtwealth <- glm.cluster(formula = multi_risc_dbl ~ district_medianwealth ,  cluster="psu", data=merg_urban, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results ALL risc univariable reg urban.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value ALL risc univariable reg urban.csv")




#univariable regressions ALL risc rural     ##



age <- glm.cluster(formula = multi_risc_dbl ~ age_grp ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results ALL risc univariable reg rural.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value ALL risc univariable reg rural.csv")

wealth <- glm.cluster(formula = multi_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results ALL risc  univariable reg rural.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value ALL risc univariable reg rural.csv")

educat <- glm.cluster(formula = multi_risc_dbl ~  educatnames ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results ALL risc univariable reg rural.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value ALL risc univariable reg rural.csv")

married <- glm.cluster(formula = multi_risc_dbl ~  married ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results ALL risc univariable reg rural.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value ALL risc univariable reg rural.csv")

sex <- glm.cluster(formula = multi_risc_dbl ~  sex ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results ALL risc univariable reg rural.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value ALL risc univariable reg rural.csv")

zone_new <- glm.cluster(formula = multi_risc_dbl ~ zone_new,  cluster="psu", data=merg_rural, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results ALL risc univariable reg rural.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value ALL risc univariable reg rural.csv")


districtwealth <- glm.cluster(formula = multi_risc_dbl ~ district_medianwealth ,  cluster="psu", data=merg_rural, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results ALL risc univariable reg rural.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value ALL risc univariable reg rural.csv")






```




```{r univariable regressions ALL risc separately for dlhs/ahs and dhs}


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))




################## zone_news as per: https://en.wikipedia.org/wiki/Administrative_divisions_of_India
merg <- mutate(merg, 
                  # Nothern
                  zone_new = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir", "North",
                                          # Northeastern
                                          ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                 # Central
                                                 ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                        # Eastern
                                                        ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                               # Western
                                                               ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                      # Southern
                                                                      ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))





DLHS_AHS <- filter(merg, svy=="AHS" | svy=="DLHS")


DLHS_AHS <- filter(DLHS_AHS, age>=18 & age<50)







###make urban rural dataset

DLHS_AHS_urban <- filter(DLHS_AHS, urban==1)
DLHS_AHS_rural <- filter(DLHS_AHS, urban==0)



#univariable regressions ALL risc URBAN     ##



age <- glm.cluster(formula = multi_risc_dbl ~ age_grp ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results ALL risc univariable reg urban DLHS_AHS only.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value ALL risc univariable reg urban DLHS_AHS only.csv")

wealth <- glm.cluster(formula = multi_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results ALL risc  univariable reg urban DLHS_AHS only.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value ALL risc univariable reg urban DLHS_AHS only.csv")

educat <- glm.cluster(formula = multi_risc_dbl ~  educatnames ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results ALL risc univariable reg urban DLHS_AHS only.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value ALL risc univariable reg urban DLHS_AHS only.csv")

married <- glm.cluster(formula = multi_risc_dbl ~  married ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results ALL risc univariable reg urban DLHS_AHS only.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value ALL risc univariable reg urban DLHS_AHS only.csv")

sex <- glm.cluster(formula = multi_risc_dbl ~  sex ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results ALL risc univariable reg urban DLHS_AHS only.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value ALL risc univariable reg urban DLHS_AHS only.csv")

zone_new <- glm.cluster(formula = multi_risc_dbl ~ zone_new,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results ALL risc univariable reg urban DLHS_AHS only.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value ALL risc univariable reg urban DLHS_AHS only.csv")


districtwealth <- glm.cluster(formula = multi_risc_dbl ~ district_medianwealth ,  cluster="psu", data=DLHS_AHS_urban, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results ALL risc univariable reg urban DLHS_AHS only.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value ALL risc univariable reg urban DLHS_AHS only.csv")




#univariable regressions ALL risc rural     ##



age <- glm.cluster(formula = multi_risc_dbl ~ age_grp ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results ALL risc univariable reg rural DLHS_AHS only.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value ALL risc univariable reg rural DLHS_AHS only.csv")

wealth <- glm.cluster(formula = multi_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results ALL risc  univariable reg rural DLHS_AHS only.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value ALL risc univariable reg rural DLHS_AHS only.csv")

educat <- glm.cluster(formula = multi_risc_dbl ~  educatnames ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results ALL risc univariable reg rural DLHS_AHS only.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value ALL risc univariable reg rural DLHS_AHS only.csv")

married <- glm.cluster(formula = multi_risc_dbl ~  married ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results ALL risc univariable reg rural DLHS_AHS only.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value ALL risc univariable reg rural DLHS_AHS only.csv")

sex <- glm.cluster(formula = multi_risc_dbl ~  sex ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results ALL risc univariable reg rural DLHS_AHS only.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value ALL risc univariable reg rural DLHS_AHS only.csv")

zone_new <- glm.cluster(formula = multi_risc_dbl ~ zone_new,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results ALL risc univariable reg rural DLHS_AHS only.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value ALL risc univariable reg rural DLHS_AHS only.csv")


districtwealth <- glm.cluster(formula = multi_risc_dbl ~ district_medianwealth ,  cluster="psu", data=DLHS_AHS_rural, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results ALL risc univariable reg rural DLHS_AHS only.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value ALL risc univariable reg rural DLHS_AHS only.csv")




 DHS <- filter(merg, svy=="DHS" )

DHS <- filter(DHS, age>=18 & age<50)



###make urban rural dataset

DHS_urban <- filter(DHS, urban==1)
DHS_rural <- filter(DHS, urban==0)



#univariable regressions ALL risc URBAN     ##



age <- glm.cluster(formula = multi_risc_dbl ~ age_grp ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results ALL risc univariable reg urban DHS only.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value ALL risc univariable reg urban DHS only.csv")

wealth <- glm.cluster(formula = multi_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results ALL risc  univariable reg urban DHS only.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value ALL risc univariable reg urban DHS only.csv")

educat <- glm.cluster(formula = multi_risc_dbl ~  educatnames ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results ALL risc univariable reg urban DHS only.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value ALL risc univariable reg urban DHS only.csv")

married <- glm.cluster(formula = multi_risc_dbl ~  married ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results ALL risc univariable reg urban DHS only.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value ALL risc univariable reg urban DHS only.csv")

sex <- glm.cluster(formula = multi_risc_dbl ~  sex ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results ALL risc univariable reg urban DHS only.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value ALL risc univariable reg urban DHS only.csv")

zone_new <- glm.cluster(formula = multi_risc_dbl ~ zone_new,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results ALL risc univariable reg urban DHS only.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value ALL risc univariable reg urban DHS only.csv")


districtwealth <- glm.cluster(formula = multi_risc_dbl ~ district_medianwealth ,  cluster="psu", data=DHS_urban, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results ALL risc univariable reg urban DHS only.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value ALL risc univariable reg urban DHS only.csv")




#univariable regressions ALL risc rural     ##



age <- glm.cluster(formula = multi_risc_dbl ~ age_grp ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results ALL risc univariable reg rural DHS only.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value ALL risc univariable reg rural DHS only.csv")

wealth <- glm.cluster(formula = multi_risc_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results ALL risc  univariable reg rural DHS only.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value ALL risc univariable reg rural DHS only.csv")

educat <- glm.cluster(formula = multi_risc_dbl ~  educatnames ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results ALL risc univariable reg rural DHS only.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value ALL risc univariable reg rural DHS only.csv")

married <- glm.cluster(formula = multi_risc_dbl ~  married ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results ALL risc univariable reg rural DHS only.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value ALL risc univariable reg rural DHS only.csv")

sex <- glm.cluster(formula = multi_risc_dbl ~  sex ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results ALL risc univariable reg rural DHS only.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value ALL risc univariable reg rural DHS only.csv")

zone_new <- glm.cluster(formula = multi_risc_dbl ~ zone_new,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results ALL risc univariable reg rural DHS only.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value ALL risc univariable reg rural DHS only.csv")


districtwealth <- glm.cluster(formula = multi_risc_dbl ~ district_medianwealth ,  cluster="psu", data=DHS_rural, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results ALL risc univariable reg rural DHS only.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value ALL risc univariable reg rural DHS only.csv")





```







```{r multivariable regressions CVD risc!!!! mit district-level fixed effects}

install.packages("DataCombine")
library(DataCombine)


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))



#####regression

###make urban rural dataset

merg_urban <- filter(merg, urban==1)
merg_rural <- filter(merg, urban==0)


####URBAN

## cvdrisc

CVDrisc <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + d_id  ,  cluster="psu", data=merg_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDrisc), confint(CVDrisc)))
write.csv(aware, "RR cvdrisc poisson urban district fixed effects.csv")

results_aware <-summary(CVDrisc)
write.csv(results_aware, "p Value cvdrisc poisson urban district fixed effects.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdrisc poisson urban district fixed effects.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdrisc poisson urban district fixed effects.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)



 write.csv(joint, "cvdrisc poisson urban district fixed effects.csv")
 

 ####Rural

## cvdrisc

CVDrisc <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + d_id  ,  cluster="psu", data=merg_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDrisc), confint(CVDrisc)))
write.csv(aware, "RR cvdrisc poisson rural district fixed effects.csv")

results_aware <-summary(CVDrisc)
write.csv(results_aware, "p Value cvdrisc poisson rural district fixed effects.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdrisc poisson rural district fixed effects.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdrisc poisson rural district fixed effects.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "cvdrisc poisson rural district fixed effects.csv")
 

 
 
 
```




```{r multivariable regressions CVD risc!!!! mit district-level fixed effects SPEEDGLM}

install.packages("DataCombine")
library(DataCombine)
library(speedglm)


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))



#####regression

###make urban rural dataset

merg_urban <- filter(merg, urban==1)
merg_rural <- filter(merg, urban==0)


####URBAN

## cvdrisc

CVDrisc <- speedglm(formula = multi_CVD_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + d_id  ,  cluster="psu", data=merg_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDrisc), confint(CVDrisc)))
write.csv(aware, "RR cvdrisc poisson urban district fixed effects.csv")

results_aware <-summary(CVDrisc)
write.csv(results_aware, "p Value cvdrisc poisson urban district fixed effects.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdrisc poisson urban district fixed effects.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdrisc poisson urban district fixed effects.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)



 write.csv(joint, "cvdrisc poisson urban district fixed effects.csv")
 

 ####Rural

## cvdrisc

CVDrisc <- speedglm(formula = multi_CVD_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + d_id  ,  cluster="psu", data=merg_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(CVDrisc), confint(CVDrisc)))
write.csv(aware, "RR cvdrisc poisson rural district fixed effects.csv")

results_aware <-summary(CVDrisc)
write.csv(results_aware, "p Value cvdrisc poisson rural district fixed effects.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR cvdrisc poisson rural district fixed effects.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value cvdrisc poisson rural district fixed effects.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "cvdrisc poisson rural district fixed effects.csv")
 

 
 
 
```



```{r univariable regressions CVD risc mit district-level fixed effects}


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))





###make urban rural dataset

merg_urban <- filter(merg, urban==1)
merg_rural <- filter(merg, urban==0)



###univariable regressions CVD risc URBAN     ###



age <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp + d_id,  cluster="psu", data=merg_urban, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results CVD risc univariable reg urban district level fixed effects.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value CVD risc univariable reg urban district level fixed effects.csv")

wealth <- glm.cluster(formula = multi_CVD_risc_dbl ~  wealth_quintile_rurb_lab + d_id,  cluster="psu", data=merg_urban, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results CVD risc  univariable reg urban district level fixed effects.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value CVD risc univariable reg urban district level fixed effects.csv")

educat <- glm.cluster(formula = multi_CVD_risc_dbl ~  educatnames + d_id,  cluster="psu", data=merg_urban, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results CVD risc univariable reg urban district level fixed effects.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value CVD risc univariable reg urban district level fixed effects.csv")

married <- glm.cluster(formula = multi_CVD_risc_dbl ~  married + d_id,  cluster="psu", data=merg_urban, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results CVD risc univariable reg urban district level fixed effects.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value CVD risc univariable reg urban district level fixed effects.csv")

sex <- glm.cluster(formula = multi_CVD_risc_dbl ~  sex + d_id,  cluster="psu", data=merg_urban, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results CVD risc univariable reg urban district level fixed effects.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value CVD risc univariable reg urban district level fixed effects.csv")





##univariable regressions CVD risc rural     #



age <- glm.cluster(formula = multi_CVD_risc_dbl ~ age_grp + d_id,  cluster="psu", data=merg_rural, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results CVD risc univariable reg rural district level fixed effects.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value CVD risc univariable reg rural district level fixed effects.csv")

wealth <- glm.cluster(formula = multi_CVD_risc_dbl ~  wealth_quintile_rurb_lab + d_id,  cluster="psu", data=merg_rural, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results CVD risc  univariable reg rural district level fixed effects.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value CVD risc univariable reg rural district level fixed effects.csv")

educat <- glm.cluster(formula = multi_CVD_risc_dbl ~  educatnames + d_id,  cluster="psu", data=merg_rural, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results CVD risc univariable reg rural district level fixed effects.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value CVD risc univariable reg rural district level fixed effects.csv")

married <- glm.cluster(formula = multi_CVD_risc_dbl ~  married + d_id,  cluster="psu", data=merg_rural, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results CVD risc univariable reg rural district level fixed effects.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value CVD risc univariable reg rural district level fixed effects.csv")

sex <- glm.cluster(formula = multi_CVD_risc_dbl ~  sex + d_id,  cluster="psu", data=merg_rural, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results CVD risc univariable reg rural district level fixed effects.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value CVD risc univariable reg rural district level fixed effects.csv")







```







```{r multivariable regressions ALL risc!!!! mit district-level fixed effects}

install.packages("DataCombine")
library(DataCombine)


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))




#####regression

###make urban rural dataset

merg_urban <- filter(merg, urban==1)
merg_rural <- filter(merg, urban==0)


####URBAN

## ALLrisc

ALLrisc <- glm.cluster(formula = multi_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + d_id,  cluster="psu", data=merg_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(ALLrisc), confint(ALLrisc)))
write.csv(aware, "RR ALLrisc poisson urban district fixed effects.csv")

results_aware <-summary(ALLrisc)
write.csv(results_aware, "p Value ALLrisc poisson urban district fixed effects.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR ALLrisc poisson urban district fixed effects.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value ALLrisc poisson urban district fixed effects.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 ALLrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)



 write.csv(joint, "ALLrisc poisson urban district fixed effects.csv")
 

 ####Rural

## ALLrisc

ALLrisc <- glm.cluster(formula = multi_risc_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + d_id ,  cluster="psu", data=merg_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(ALLrisc), confint(ALLrisc)))
write.csv(aware, "RR ALLrisc poisson rural district fixed effects.csv")

results_aware <-summary(ALLrisc)
write.csv(results_aware, "p Value ALLrisc poisson rural district fixed effects.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/RR ALLrisc poisson rural district fixed effects.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/p Value ALLrisc poisson rural district fixed effects.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 ALLrisc"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 23)
joint <- InsertRow(joint, NewRow = noref, RowNum = 23)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 18)
joint <- InsertRow(joint, NewRow = age, RowNum = 11)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 7)
joint <- InsertRow(joint, NewRow = noref, RowNum = 7)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "ALLrisc poisson rural district fixed effects.csv")
 


```
 
 
 
 
 



```{r univariable regressions ALL risc mit district-level fixed effects}


        merg <- merg %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        merg <- merg %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))



###make urban rural dataset

merg_urban <- filter(merg, urban==1)
merg_rural <- filter(merg, urban==0)



#univariable regressions ALL risc URBAN     ##



age <- glm.cluster(formula = multi_risc_dbl ~ age_grp + d_id,  cluster="psu", data=merg_urban, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results ALL risc univariable reg urban district fixed effects.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value ALL risc univariable reg urban district fixed effects.csv")

wealth <- glm.cluster(formula = multi_risc_dbl ~  wealth_quintile_rurb_lab + d_id,  cluster="psu", data=merg_urban, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results ALL risc  univariable reg urban district fixed effects.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value ALL risc univariable reg urban district fixed effects.csv")

educat <- glm.cluster(formula = multi_risc_dbl ~  educatnames + d_id,  cluster="psu", data=merg_urban, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results ALL risc univariable reg urban district fixed effects.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value ALL risc univariable reg urban district fixed effects.csv")

married <- glm.cluster(formula = multi_risc_dbl ~  married + d_id,  cluster="psu", data=merg_urban, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results ALL risc univariable reg urban district fixed effects.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value ALL risc univariable reg urban district fixed effects.csv")

sex <- glm.cluster(formula = multi_risc_dbl ~  sex + d_id,  cluster="psu", data=merg_urban, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results ALL risc univariable reg urban district fixed effects.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value ALL risc univariable reg urban district fixed effects.csv")




#univariable regressions ALL risc rural     ##



age <- glm.cluster(formula = multi_risc_dbl ~ age_grp + d_id,  cluster="psu", data=merg_rural, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results ALL risc univariable reg rural district fixed effects.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value ALL risc univariable reg rural district fixed effects.csv")

wealth <- glm.cluster(formula = multi_risc_dbl ~  wealth_quintile_rurb_lab + d_id,  cluster="psu", data=merg_rural, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results ALL risc  univariable reg rural district fixed effects.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value ALL risc univariable reg rural district fixed effects.csv")

educat <- glm.cluster(formula = multi_risc_dbl ~  educatnames + d_id,  cluster="psu", data=merg_rural, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results ALL risc univariable reg rural district fixed effects.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value ALL risc univariable reg rural district fixed effects.csv")

married <- glm.cluster(formula = multi_risc_dbl ~  married + d_id,  cluster="psu", data=merg_rural, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results ALL risc univariable reg rural district fixed effects.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value ALL risc univariable reg rural district fixed effects.csv")

sex <- glm.cluster(formula = multi_risc_dbl ~  sex + d_id,  cluster="psu", data=merg_rural, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results ALL risc univariable reg rural district fixed effects.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value ALL risc univariable reg rural district fixed effects.csv")






```





```{r Heatmap Multiple Morbidities}



#####Number of persons with one of these diseases

length(which(merg$ex_anemia_ind==1 | merg$obese==1 | merg$ex_diab_narrow_ind==1| merg$ex_htn_narrow_ind==1 | merg$sev_underweight==1 | merg$smoke==1)) ###992251


######HEATMAPS risc WEALTH



multi_heatmap <- merg %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(multi_risc_dbl, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat


#####weath Hypertension_anemia

######HEATMAPS MULTIPLE risc educat



multi_heatmap <- merg %>%
  filter(is.na(educatnames_few)==FALSE & is.na(age)==FALSE) %>% 
  group_by( educatnames_few, age_grp, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(multi_risc_dbl, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( educatnames_few, age_grp, urban_lab, multi_morbid_indiv, educatnames_few, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=educatnames_few, y=age_grp)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Education",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(4/6)
multi_heat

```

```{r comorbidities wealth heatmaps}

#####wealth comorbidities heatmaps

######Wealth Hypertension Anemia

multi_heatmap <- merg %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp_old, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(Hypertension_Anemia, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp_old, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp_old)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat


######Wealthobese anemia

multi_heatmap <- merg %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp_old, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(Obese_Anemia, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp_old, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp_old)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat


####wealth diabetes anemia


multi_heatmap <- merg %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp_old, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(Diabetes_Anemia, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp_old, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp_old)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat


####wealth Anemia smoking


multi_heatmap <- merg %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp_old, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(Anemia_Smoking, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp_old, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp_old)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat

####wealth  smoking


multi_heatmap <- merg %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp_old, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(smoke_dbl, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp_old, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp_old)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat

####wealth underweight

multi_heatmap <- merg %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp_old, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(sev_underweight_dbl, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp_old, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp_old)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat

####wealth obese smoking

multi_heatmap <- merg %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp_old, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(Obese_Smoking, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp_old, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp_old)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat






####wealth diabetes hypertension


multi_heatmap <- merg %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp_old, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(Diabetes_Hypertension, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp_old, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp_old)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat
 





######HEATMAPS MULTIPLE MORBIDITIES WEALTH WOMEN



multi_heatmap <- merg_women %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(multi_morbid_dbl, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat

######HEATMAPS MULTIPLE MORBIDITIES WEALTH MEN



multi_heatmap <- merg_men %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(multi_morbid_dbl, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat

#####Education

multi_heatmap <- merg %>%
  filter(is.na(educatnames)==FALSE & is.na(age)==FALSE) %>% 
  group_by( educatnames, age_grp, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(multi_morbid_dbl, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( educatnames, age_grp, urban_lab, multi_morbid_indiv, educatnames, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=educatnames, y=age_grp)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat



#####Education Hypertension Anemia

multi_heatmap <- merg %>%
  filter(is.na(educatnames)==FALSE & is.na(age)==FALSE) %>% 
  group_by( educatnames, age_grp, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(Hypertension_Anemia, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( educatnames, age_grp, urban_lab, multi_morbid_indiv, educatnames, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=educatnames, y=age_grp)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat







```


```{r Comorbidities per zone_new}

merg <- mutate(merg, 
                     # Nothern
                     zone_new = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan", "North",
                                             # Northeastern
                                             ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                    # Central
                                                    ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                           # Eastern
                                                           ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                                  # Western
                                                                  ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra", "West",
                                                                         # Southern
                                                                         ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))


North <- filter(merg, zone_new=="North")
Northeast<- filter(merg, zone_new=="Northeast")
Central <- filter(merg, zone_new=="Central")
East<- filter(merg, zone_new=="East")
West <- filter(merg, zone_new=="West")
South <- filter(merg, zone_new=="South")

###North

  heatcols <- hsv(1, 1, seq(1,0,length.out = 14))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(North$ex_diab_narrow_ind==1& North$ex_htn_narrow_ind==1))),
             'Diabetes&Anemia' = (length(which(North$ex_diab_narrow_ind==1& North$ex_anemia_ind==1))),
             'Obese&Diabetes'= (length(which(North$ex_diab_narrow_ind==1& North$obese==1))),
             'Smoking&Diabetes'= (length(which(North$ex_diab_narrow_ind==1& North$smoke==1))),
             'sev_Thinness&Diabetes'= (length(which(North$ex_diab_narrow_ind==1& North$sev_underweight==1))),
             'Hypertension&Anemia'= (length(which(North$ex_htn_narrow_ind==1& North$ex_anemia_ind==1))) ,
             'Obese&Hypertension'= (length(which(North$ex_htn_narrow_ind==1& North$obese==1))),
             'sev_Thinness&Hypertension'= (length(which(North$ex_htn_narrow_ind==1& North$sev_underweight==1))),
             'Smoking&Hypertension'= (length(which(North$ex_htn_narrow_ind==1& North$smoke==1))),
             'Obese&Anemia'= (length(which(North$ex_anemia_ind==1& North$obese==1))),
             'Obese&Smoking'= (length(which(North$smoke==1& North$obese==1))),
             'Smoking&sev_Thinness'= (length(which(North$sev_underweight==1& North$smoke==1))),
             'Anemia&sev_Thinness'= (length(which(North$sev_underweight==1& North$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(North$smoke==1& North$ex_anemia_ind==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
  ####Northeast
  
    heatcols <- hsv(1, 1, seq(1,0,length.out = 14))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(Northeast$ex_diab_narrow_ind==1& Northeast$ex_htn_narrow_ind==1))),
             'Diabetes&Anemia' = (length(which(Northeast$ex_diab_narrow_ind==1& Northeast$ex_anemia_ind==1))),
             'Obese&Diabetes'= (length(which(Northeast$ex_diab_narrow_ind==1& Northeast$obese==1))),
             'Smoking&Diabetes'= (length(which(Northeast$ex_diab_narrow_ind==1& Northeast$smoke==1))),
             'sev_Thinness&Diabetes'= (length(which(Northeast$ex_diab_narrow_ind==1& Northeast$sev_underweight==1))),
             'Hypertension&Anemia'= (length(which(Northeast$ex_htn_narrow_ind==1& Northeast$ex_anemia_ind==1))) ,
             'Obese&Hypertension'= (length(which(Northeast$ex_htn_narrow_ind==1& Northeast$obese==1))),
             'sev_Thinness&Hypertension'= (length(which(Northeast$ex_htn_narrow_ind==1& Northeast$sev_underweight==1))),
             'Smoking&Hypertension'= (length(which(Northeast$ex_htn_narrow_ind==1& Northeast$smoke==1))),
             'Obese&Anemia'= (length(which(Northeast$ex_anemia_ind==1& Northeast$obese==1))),
             'Obese&Smoking'= (length(which(Northeast$smoke==1& Northeast$obese==1))),
             'Smoking&sev_Thinness'= (length(which(Northeast$sev_underweight==1& Northeast$smoke==1))),
             'Anemia&sev_Thinness'= (length(which(Northeast$sev_underweight==1& Northeast$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(Northeast$smoke==1& Northeast$ex_anemia_ind==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
  #####Central
  
    heatcols <- hsv(1, 1, seq(1,0,length.out = 14))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(Central$ex_diab_narrow_ind==1& Central$ex_htn_narrow_ind==1))),
             'Diabetes&Anemia' = (length(which(Central$ex_diab_narrow_ind==1& Central$ex_anemia_ind==1))),
             'Obese&Diabetes'= (length(which(Central$ex_diab_narrow_ind==1& Central$obese==1))),
             'Smoking&Diabetes'= (length(which(Central$ex_diab_narrow_ind==1& Central$smoke==1))),
             'sev_Thinness&Diabetes'= (length(which(Central$ex_diab_narrow_ind==1& Central$sev_underweight==1))),
             'Hypertension&Anemia'= (length(which(Central$ex_htn_narrow_ind==1& Central$ex_anemia_ind==1))) ,
             'Obese&Hypertension'= (length(which(Central$ex_htn_narrow_ind==1& Central$obese==1))),
             'sev_Thinness&Hypertension'= (length(which(Central$ex_htn_narrow_ind==1& Central$sev_underweight==1))),
             'Smoking&Hypertension'= (length(which(Central$ex_htn_narrow_ind==1& Central$smoke==1))),
             'Obese&Anemia'= (length(which(Central$ex_anemia_ind==1& Central$obese==1))),
             'Obese&Smoking'= (length(which(Central$smoke==1& Central$obese==1))),
             'Smoking&sev_Thinness'= (length(which(Central$sev_underweight==1& Central$smoke==1))),
             'Anemia&sev_Thinness'= (length(which(Central$sev_underweight==1& Central$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(Central$smoke==1& Central$ex_anemia_ind==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
  ####East
  
    heatcols <- hsv(1, 1, seq(1,0,length.out = 14))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(East$ex_diab_narrow_ind==1& East$ex_htn_narrow_ind==1))),
             'Diabetes&Anemia' = (length(which(East$ex_diab_narrow_ind==1& East$ex_anemia_ind==1))),
             'Obese&Diabetes'= (length(which(East$ex_diab_narrow_ind==1& East$obese==1))),
             'Smoking&Diabetes'= (length(which(East$ex_diab_narrow_ind==1& East$smoke==1))),
             'sev_Thinness&Diabetes'= (length(which(East$ex_diab_narrow_ind==1& East$sev_underweight==1))),
             'Hypertension&Anemia'= (length(which(East$ex_htn_narrow_ind==1& East$ex_anemia_ind==1))) ,
             'Obese&Hypertension'= (length(which(East$ex_htn_narrow_ind==1& East$obese==1))),
             'sev_Thinness&Hypertension'= (length(which(East$ex_htn_narrow_ind==1& East$sev_underweight==1))),
             'Smoking&Hypertension'= (length(which(East$ex_htn_narrow_ind==1& East$smoke==1))),
             'Obese&Anemia'= (length(which(East$ex_anemia_ind==1& East$obese==1))),
             'Obese&Smoking'= (length(which(East$smoke==1& East$obese==1))),
             'Smoking&sev_Thinness'= (length(which(East$sev_underweight==1& East$smoke==1))),
             'Anemia&sev_Thinness'= (length(which(East$sev_underweight==1& East$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(East$smoke==1& East$ex_anemia_ind==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
  ####West
  
    heatcols <- hsv(1, 1, seq(1,0,length.out = 14))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(West$ex_diab_narrow_ind==1& West$ex_htn_narrow_ind==1))),
             'Diabetes&Anemia' = (length(which(West$ex_diab_narrow_ind==1& West$ex_anemia_ind==1))),
             'Obese&Diabetes'= (length(which(West$ex_diab_narrow_ind==1& West$obese==1))),
             'Smoking&Diabetes'= (length(which(West$ex_diab_narrow_ind==1& West$smoke==1))),
             'sev_Thinness&Diabetes'= (length(which(West$ex_diab_narrow_ind==1& West$sev_underweight==1))),
             'Hypertension&Anemia'= (length(which(West$ex_htn_narrow_ind==1& West$ex_anemia_ind==1))) ,
             'Obese&Hypertension'= (length(which(West$ex_htn_narrow_ind==1& West$obese==1))),
             'sev_Thinness&Hypertension'= (length(which(West$ex_htn_narrow_ind==1& West$sev_underweight==1))),
             'Smoking&Hypertension'= (length(which(West$ex_htn_narrow_ind==1& West$smoke==1))),
             'Obese&Anemia'= (length(which(West$ex_anemia_ind==1& West$obese==1))),
             'Obese&Smoking'= (length(which(West$smoke==1& West$obese==1))),
             'Smoking&sev_Thinness'= (length(which(West$sev_underweight==1& West$smoke==1))),
             'Anemia&sev_Thinness'= (length(which(West$sev_underweight==1& West$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(West$smoke==1& West$ex_anemia_ind==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
  ###South
  
    heatcols <- hsv(1, 1, seq(1,0,length.out = 14))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(South$ex_diab_narrow_ind==1& South$ex_htn_narrow_ind==1))),
             'Diabetes&Anemia' = (length(which(South$ex_diab_narrow_ind==1& South$ex_anemia_ind==1))),
             'Obese&Diabetes'= (length(which(South$ex_diab_narrow_ind==1& South$obese==1))),
             'Smoking&Diabetes'= (length(which(South$ex_diab_narrow_ind==1& South$smoke==1))),
             'sev_Thinness&Diabetes'= (length(which(South$ex_diab_narrow_ind==1& South$sev_underweight==1))),
             'Hypertension&Anemia'= (length(which(South$ex_htn_narrow_ind==1& South$ex_anemia_ind==1))) ,
             'Obese&Hypertension'= (length(which(South$ex_htn_narrow_ind==1& South$obese==1))),
             'sev_Thinness&Hypertension'= (length(which(South$ex_htn_narrow_ind==1& South$sev_underweight==1))),
             'Smoking&Hypertension'= (length(which(South$ex_htn_narrow_ind==1& South$smoke==1))),
             'Obese&Anemia'= (length(which(South$ex_anemia_ind==1& South$obese==1))),
             'Obese&Smoking'= (length(which(South$smoke==1& South$obese==1))),
             'Smoking&sev_Thinness'= (length(which(South$sev_underweight==1& South$smoke==1))),
             'Anemia&sev_Thinness'= (length(which(South$sev_underweight==1& South$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(South$smoke==1& South$ex_anemia_ind==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
  


```

```{r Table1 Summary}

merg$smoke <- as.factor(merg$smoke)
merg$csmkls_tb <- as.factor(merg$csmkls_tb)


###Table1


table1names <- c( "age_grp",
                 "educatnames", "wealth_quintile_rurb_lab",  "urban_lab","marriednames", "obese",  "ex_htn_narrow_ind", "ex_diab_narrow_ind", "smoke","csmkls_tb")




totalwithmiss <- CreateTableOne(vars = table1names, data=merg, includeNA=T)
total <- print(totalwithmiss)

write.csv(total, file="merg summary.csv")

sexwithmiss <- CreateTableOne(vars = table1names, data=merg, strata=c("female_lab"), includeNA=T)
sexdhs_nomiss <- print(sexwithmiss)

write.csv(sexdhs_nomiss, file= "merg summary per sex.csv")


```



```{r Table1 Summary for missing}


######FILTER out those that have missing 


missingvalues <- filter(DHLSandAHS, is.na(DHLSandAHS$obese)==T | is.na(DHLSandAHS$ex_diab_narrow_ind)==T | is.na(DHLSandAHS$ex_htn_narrow_ind)==T )







Missing<- filter(dhs, is.na(dhs$obese)==T | is.na(dhs$ex_diab_narrow_ind)==T | is.na(dhs$ex_htn_narrow_ind)==T )

####new anemia variable

Missing <- dplyr::mutate(Missing,
                           mild_anemia = ifelse((sex = 1 & ex_hb_ind < 12 & ex_hb_ind >= 11) | (sex = 0 & ex_hb_ind < 13 &ex_hb_ind >= 11 ), 1, 0))
Missing$mild_anemia <- factor(Missing$mild_anemia, levels = c("0", "1"))

Missing <- dplyr::mutate(Missing,
                            moderate_anemia = ifelse((sex = 1 & ex_hb_ind < 11 & ex_hb_ind >= 8) | (sex = 0 & ex_hb_ind < 11 & ex_hb_ind >= 8 ), 1, 0))
Missing$moderate_anemia <- factor(Missing$moderate_anemia, levels = c("0", "1"))

Missing <- dplyr::mutate(Missing,
                            severe_anemia = ifelse((sex = 1 & ex_hb_ind < 8) | (sex = 0 & ex_hb_ind < 8 ), 1, 0))
Missing$severe_anemia <- factor(Missing$severe_anemia, levels = c("0", "1")) 


Missing <- mutate(Missing, 
               ex_anemia_ind = ifelse(moderate_anemia==1 | severe_anemia==1,1,0))

length(which(Missing$ex_anemia_ind==1))

#####create correct morbiditiy categories
Missing <- mutate(Missing, 
               sev_underweight = ifelse(bmi<16,1,0))

Missing <- mutate(Missing, 
               obese = ifelse(bmi>=27.5,1,0))

######make new smoking variable since csmoke is incorrect for dhs

Missing <- mutate(Missing, 
                     smoke = ifelse(csmoke==1 & svy=="AHS", 1, 
                                    ifelse(csmoke==1 & svy=="DLHS",1,
                                           ifelse(tobacco_smoked==1 & svy=="DHS",1,0))))

Missing <- filter(Missing, is.na(Missing$obese)==T | is.na(Missing$sev_underweight)==T | is.na(Missing$ex_diab_narrow_ind)==T | is.na(Missing$ex_htn_narrow_ind)==T | is.na(Missing$smoke)==T | is.na(Missing$csmkls_tb)==T )


#####create correct characteristics

Missing <- dplyr::mutate(Missing, age_grp = ifelse(age<=24 , "15-24", 
                                                             ifelse(age>24 &  age<=34, "25-34",
                                                                    ifelse(age>34 &  age<=44, "35-44",
                                                                           ifelse(age>44 &  age<=54, "45-54", 
                                                                                  ifelse(age>54 &  age<=64, "55-64",
                                                                                         ifelse(age>64, "65+", NA)))))))

                                                                              
Missing$age_grp <- factor(Missing$age_grp, levels = c("15-24", "25-34", "35-44", "45-54","55-64", "65+"))
Missing <- within(Missing, age_grp <- relevel(age_grp, ref = "15-24"))


Missing <- dplyr::mutate(Missing, age_grp_old = ifelse(age<=25 , "15-25", 
                                                             ifelse(age>25 &  age<=35, "26-35",
                                                                    ifelse(age>35 &  age<=45, "36-45",
                                                                           ifelse(age>45 &  age<=55, "46-55", 
                                                                                  ifelse(age>55 &  age<=65, "56-65",
                                                                                         ifelse(age>65 &  age<=75, "66-75",
                                                                                         ifelse(age>75, "76+", NA))))))))

                                                                              
Missing$age_grp_old <- factor(Missing$age_grp_old, levels = c("15-25", "26-35", "36-45", "46-55","56-65", "66-75", "76+"))
Missing <- within(Missing, age_grp_old <- relevel(age_grp_old, ref = "15-25"))



Missing <- mutate(Missing, 
                     rural = ifelse(urban==1, 0, 1),
                     female = ifelse(sex==1, 1, 0),
                     male = ifelse(sex==1, 0, 1))
Missing$rural <- as.factor(Missing$rural)
Missing$female <- as.factor(Missing$female)
Missing$male <- as.factor(Missing$male)

Missing <- dplyr::mutate(Missing, educatnames = ifelse(educat_lcl==0, "No formal education", 
                                                             ifelse(educat_lcl==1, "< Primary school",
                                                                    ifelse(educat_lcl==2, "Primary school",
                                                                           ifelse(educat_lcl==3, "Middle school",
                                                                                  ifelse(educat_lcl==4, "Secondary school",
                                                                                          ifelse(educat_lcl==5, "> Secondary school",NA))))))) 
Missing$educatnames <- factor(Missing$educatnames, levels = c("No formal education", "< Primary school", "Primary school", "Middle school", "Secondary school", "> Secondary school", NA))
Missing <- within(Missing, educatnames<- relevel(educatnames, ref = "No formal education"))


Missing <- dplyr::mutate(Missing, educatnames_few = ifelse(educat_lcl==0 | educat_lcl==1, "< Primary school", 
                                                            ifelse(educat_lcl==2 | educat_lcl==3, "< Secondary school",
                                                                           ifelse(educat_lcl==4, "Secondary school",
                                                                                  ifelse(educat_lcl==5, "> Secondary school",NA))))) 
Missing$educatnames_few <- factor(Missing$educatnames_few, levels = c("< Primary school", "< Secondary school", "Secondary school", "> Secondary school", NA))
Missing <- within(Missing, educatnames_few<- relevel(educatnames_few, ref = "< Primary school"))




Missing <- Missing %>% 
  mutate(female_lab = as.factor(ifelse(is.na(female) == TRUE, NA, 
                                       ifelse(female == 1, "Female", "Male"))),
         urban_lab = as.factor(ifelse(is.na(rural) == TRUE, NA, 
                                      ifelse(rural == 1, "Rural", "Urban"))),
         wealth_quintile_rurb_lab = as.factor(ifelse(is.na(wealth_quintile_rurb) == TRUE, NA, 
                                                     ifelse( wealth_quintile_rurb == 1, "Q1 (Poorest)", 
                                                             ifelse(wealth_quintile_rurb == 2, "Q2",
                                                                    ifelse(wealth_quintile_rurb == 3, "Q3",
                                                                           ifelse(wealth_quintile_rurb == 4, "Q4",
                                                                                  ifelse(wealth_quintile_rurb == 5, "Q5 (Richest)", NA))))))))

Missing$wealth_quintile_rurb_lab <- as.factor(Missing$wealth_quintile_rurb_lab)
Missing<- within(Missing, wealth_quintile_rurb_lab <- relevel(wealth_quintile_rurb_lab, ref = "Q1 (Poorest)"))

Missing$obese <- as.factor(Missing$obese)
Missing$csmoke <- as.factor(Missing$csmoke)
Missing$ex_htn_narrow_ind <- as.factor(Missing$ex_htn_narrow_ind)
Missing$ex_diab_narrow_ind <- as.factor(Missing$ex_diab_narrow_ind)
Missing$sev_underweight <- as.factor(Missing$sev_underweight)
Missing$ex_anemia_ind <- as.factor(Missing$ex_anemia_ind)
Missing$educat <- as.factor(Missing$educat)


Missing$smoke <- as.factor(Missing$smoke)

Missing <- mutate( Missing,
                      married = ifelse( Missing$marital==2, 1, 0))

Missing$married <- as.factor(Missing$married)


Missing <- mutate( Missing,
                      marriednames = ifelse( Missing$marital==2, "Married","Not married"))

Missing$marriednames <- as.factor(Missing$marriednames)

Missing$csmkls_tb <- as.factor(Missing$csmkls_tb)

Missing <- within(Missing, marriednames<- relevel(marriednames, ref = "Not married"))


###Table1


table1names <- c( "age_grp",
                 "educatnames", "wealth_quintile_rurb_lab",  "urban_lab","marriednames", "obese", "smoke", "ex_htn_narrow_ind", "ex_diab_narrow_ind", "sev_underweight","csmkls_tb")




totalwithmiss <- CreateTableOne(vars = table1names, data=Missing, includeNA=T)
total <- print(totalwithmiss)

write.csv(total, file="Missing summary.csv")

sexwithmiss <- CreateTableOne(vars = table1names, data=Missing, strata=c("female_lab"), includeNA=T)
sexdhs_nomiss <- print(sexwithmiss)

write.csv(sexdhs_nomiss, file= "Missing summary per sex.csv")


```


```{r Subgroup analysis who with who}
```

```{r 2 way interactions upsetr ALL RISC FACTORS}


####2 interactions national



  svy_all <- merg %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(diabetes_hypertension,
diabetes_smoke,
diabetes_smokeless,
diabetes_obese,
diabetes_underweight,
hypertension_smoke,
hypertension_smokeless,
hypertension_obese,
hypertension_underweight,
smoke_smokeless ,
smoke_obese ,
smoke_underweight,
smokeless_obese,
smokeless_underweight,
obese_underweight))
  
  prevtot2risc <- svy_all %>%
    summarize(diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100,
              diabetes_smoke_prop = survey_mean(diabetes_smoke, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_smokeless_prop = survey_mean(diabetes_smokeless, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_obese_prop = survey_mean(diabetes_obese, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_underweight_prop = survey_mean(diabetes_underweight, proportion=TRUE, vartype = "ci")*100 ,
              hypertension_smoke_prop = survey_mean(hypertension_smoke, proportion=TRUE, vartype = "ci")*100 ,
              hypertension_smokeless_prop = survey_mean(hypertension_smokeless, proportion=TRUE, vartype = "ci")*100 ,
              smoke_smokeless_prop = survey_mean(smoke_smokeless, proportion=TRUE, vartype = "ci")*100 ,
              smoke_obese_prop = survey_mean(smoke_obese, proportion=TRUE, vartype = "ci")*100 ,
              smoke_underweight_prop = survey_mean(smoke_underweight, proportion=TRUE, vartype = "ci")*100 ,
              smokeless_obese_prop = survey_mean(smokeless_obese, proportion=TRUE, vartype = "ci")*100 ,
              smokeless_underweight_prop = survey_mean(smokeless_underweight, proportion=TRUE, vartype = "ci")*100 ,
                 obese_underweight_prop = survey_mean(obese_underweight, proportion=TRUE, vartype = "ci")*100)
     


write.csv(prevtot2risc, "prevalence 2way interactions national.csv")

   prevtot2riscmissingones <- svy_all %>%
    summarize(diabetes_smokeless_prop = survey_mean(diabetes_smokeless, proportion=TRUE, vartype = "ci")*100 ,
              hypertension_obese_prop = survey_mean(hypertension_obese, proportion=TRUE, vartype = "ci")*100 ,
              hypertension_underweight_prop = survey_mean(hypertension_underweight, proportion=TRUE, vartype = "ci")*100)

write.csv(prevtot2riscmissingones, "prevalence 2way interactions risc missing ones .csv")


prevalence.2way.interactions.national <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/prevalence 2way interactions national.csv")


inter <- prevalence.2way.interactions.national

inter2 <- read.csv("prevalence 2way interactions risc missing ones .csv")

    heatcols <- hsv(1, 1, seq(1,0,length.out = 14))
  
  Upset <- c('Diabetes&Hypertension' = (inter$diabetes_hypertension_prop)/100*2320799,
             'Diabetes&Smoking' = (inter$diabetes_smoke_prop)/100*2320799,
             'Diabetes&Obese'= (inter$diabetes_obese_prop)/100*2320799,
              'Diabetes&Smokeless tobacco'= (inter2$diabetes_smokeless_prop)/100*2320799,
             'Diabetes&severe Underweight'= (inter$diabetes_underweight_prop)/100*2320799,
             'Hypertension&Smoking'= (inter$hypertension_smoke_prop)/100*2320799,
             'Hypertension&Smokeless tobacco'= (inter$hypertension_smokeless_prop)/100*2320799,
              'Hypertension&Obese'= (inter2$hypertension_obese_prop)/100*2320799,
              'Hypertension&severe Underweight'= (inter2$hypertension_underweight_prop)/100*2320799,
             'Smoking&Smokeless tobacco'= (inter$smoke_smokeless_prop)/100*2320799,
             'Smoking&Obese'= (inter$smoke_obese_prop)/100*2320799,
             'Smoking&severe Underweight'= (inter$smoke_underweight_prop)/100*2320799,
             'Smokeless tobacco&Obese'= (inter$smokeless_obese_prop)/100*2320799,
             'Smokeless tobacco&severe Underweight'= (inter$smokeless_underweight_prop)/100*2320799)
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals",main.bar.color=heatcols)  
  
  
   ######FORMATTING FOR TABLE

`10.year.age.group.prevalence.multirisc` <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/prevalence 2way interactions national.csv")
aware <- `10.year.age.group.prevalence.multirisc`


aware <- mutate(aware,
                diabetes_hypertension_prop = round(diabetes_hypertension_prop,2),
                diabetes_smoke_prop = round(diabetes_smoke_prop,2),
                diabetes_smokeless_prop = round(diabetes_smokeless_prop,2),
                diabetes_obese_prop = round(diabetes_obese_prop,2),
                diabetes_underweight_prop = round(diabetes_underweight_prop,2),
                hypertension_smoke_prop = round(hypertension_smoke_prop,2),
                hypertension_smokeless_prop = round(hypertension_smokeless_prop,2),
                hypertension_obese_prop = round(hypertension_obese_prop,2),
                hypertension_underweight_prop = round(hypertension_underweight_prop,2),
                smoke_smokeless_prop = round(smoke_smokeless_prop,2),
                smoke_obese_prop = round(smoke_obese_prop,2),
                smoke_underweight_prop = round(smoke_underweight_prop,2),
                smokeless_obese_prop = round(smokeless_obese_prop,2),
                smokeless_underweight_prop = round(smokeless_underweight_prop,2),
                obese_underweight_prop = round(obese_underweight_prop,2),
                  diabetes_hypertension_prop_low = round(diabetes_hypertension_prop_low,2),
                diabetes_smoke_prop_low = round(diabetes_smoke_prop_low,2),
                diabetes_smokeless_prop_low = round(diabetes_smokeless_prop_low,2),
                diabetes_obese_prop_low = round(diabetes_obese_prop_low,2),
                diabetes_underweight_prop_low = round(diabetes_underweight_prop_low,2),
                hypertension_smoke_prop_low = round(hypertension_smoke_prop_low,2),
                hypertension_smokeless_prop_low = round(hypertension_smokeless_prop_low,2),
                hypertension_obese_prop_low = round(hypertension_obese_prop_low,2),
                hypertension_underweight_prop_low = round(hypertension_underweight_prop_low,2),
                smoke_smokeless_prop_low = round(smoke_smokeless_prop_low,2),
                smoke_obese_prop_low = round(smoke_obese_prop_low,2),
                smoke_underweight_prop_low = round(smoke_underweight_prop_low,2),
                smokeless_obese_prop_low = round(smokeless_obese_prop_low,2),
                smokeless_underweight_prop_low = round(smokeless_underweight_prop_low,2),
                obese_underweight_prop_low = round(obese_underweight_prop_low,2),
                     diabetes_hypertension_prop_upp = round(diabetes_hypertension_prop_upp,2),
                diabetes_smoke_prop_upp = round(diabetes_smoke_prop_upp,2),
                diabetes_smokeless_prop_upp = round(diabetes_smokeless_prop_upp,2),
                diabetes_obese_prop_upp = round(diabetes_obese_prop_upp,2),
                diabetes_underweight_prop_upp = round(diabetes_underweight_prop_upp,2),
                hypertension_smoke_prop_upp = round(hypertension_smoke_prop_upp,2),
                hypertension_smokeless_prop_upp = round(hypertension_smokeless_prop_upp,2),
                hypertension_obese_prop_upp = round(hypertension_obese_prop_upp,2),
                hypertension_underweight_prop_upp = round(hypertension_underweight_prop_upp,2),
                smoke_smokeless_prop_upp = round(smoke_smokeless_prop_upp,2),
                smoke_obese_prop_upp = round(smoke_obese_prop_upp,2),
                smoke_underweight_prop_upp = round(smoke_underweight_prop_upp,2),
                smokeless_obese_prop_upp = round(smokeless_obese_prop_upp,2),
                smokeless_underweight_prop_upp = round(smokeless_underweight_prop_upp,2),
                obese_underweight_prop_upp = round(obese_underweight_prop_upp,2))



                
                aware$diabetes_hypertension_prop <- sprintf("%.2f", aware$diabetes_hypertension_prop)
                 aware$diabetes_smoke_prop <- sprintf("%.2f", aware$diabetes_smoke_prop)
                 aware$diabetes_smokeless_prop <- sprintf("%.2f", aware$diabetes_smokeless_prop)
                 aware$diabetes_obese_prop <- sprintf("%.2f", aware$diabetes_obese_prop)
                 aware$diabetes_underweight_prop <- sprintf("%.2f", aware$diabetes_underweight_prop)
                 aware$hypertension_smoke_prop <- sprintf("%.2f", aware$hypertension_smoke_prop)
                 aware$hypertension_smokeless_prop <- sprintf("%.2f", aware$hypertension_smokeless_prop)
                aware$hypertension_obese_prop <- sprintf("%.2f", aware$hypertension_obese_prop)
                 aware$hypertension_underweight_prop <- sprintf("%.2f", aware$hypertension_underweight_prop)
                 aware$smoke_smokeless_prop <- sprintf("%.2f", aware$smoke_smokeless_prop)
                 aware$smoke_obese_prop <- sprintf("%.2f", aware$smoke_obese_prop)
                 aware$smoke_underweight_prop <- sprintf("%.2f", aware$smoke_underweight_prop)
                 aware$smokeless_obese_prop <- sprintf("%.2f", aware$smokeless_obese_prop)
                 aware$smokeless_underweight_prop <- sprintf("%.2f", aware$smokeless_underweight_prop)
                 aware$obese_underweight_prop <- sprintf("%.2f", aware$obese_underweight_prop)
                   aware$diabetes_hypertension_prop_low <- sprintf("%.2f", aware$diabetes_hypertension_prop_low)
                 aware$diabetes_smoke_prop_low <- sprintf("%.2f", aware$diabetes_smoke_prop_low)
                 aware$diabetes_smokeless_prop_low <- sprintf("%.2f", aware$diabetes_smokeless_prop_low)
                 aware$diabetes_obese_prop_low <- sprintf("%.2f", aware$diabetes_obese_prop_low)
                 aware$diabetes_underweight_prop_low <- sprintf("%.2f", aware$diabetes_underweight_prop_low)
                 aware$hypertension_smoke_prop_low <- sprintf("%.2f", aware$hypertension_smoke_prop_low)
                 aware$hypertension_smokeless_prop_low <- sprintf("%.2f", aware$hypertension_smokeless_prop_low)
                 aware$hypertension_obese_prop_low <- sprintf("%.2f", aware$hypertension_obese_prop_low)
                 aware$hypertension_underweight_prop_low <- sprintf("%.2f", aware$hypertension_underweight_prop_low)
                 aware$smoke_smokeless_prop_low <- sprintf("%.2f", aware$smoke_smokeless_prop_low)
                 aware$smoke_obese_prop_low <- sprintf("%.2f", aware$smoke_obese_prop_low)
                 aware$smoke_underweight_prop_low <- sprintf("%.2f", aware$smoke_underweight_prop_low)
                 aware$smokeless_obese_prop_low <- sprintf("%.2f", aware$smokeless_obese_prop_low)
                 aware$smokeless_underweight_prop_low <- sprintf("%.2f", aware$smokeless_underweight_prop_low)
                 aware$obese_underweight_prop_low <- sprintf("%.2f", aware$obese_underweight_prop_low)
                      aware$diabetes_hypertension_prop_upp <- sprintf("%.2f", aware$diabetes_hypertension_prop_upp)
                 aware$diabetes_smoke_prop_upp <- sprintf("%.2f", aware$diabetes_smoke_prop_upp)
                 aware$diabetes_smokeless_prop_upp <- sprintf("%.2f", aware$diabetes_smokeless_prop_upp)
                 aware$diabetes_obese_prop_upp <- sprintf("%.2f", aware$diabetes_obese_prop_upp)
                 aware$diabetes_underweight_prop_upp <- sprintf("%.2f", aware$diabetes_underweight_prop_upp)
                 aware$hypertension_smoke_prop_upp <- sprintf("%.2f", aware$hypertension_smoke_prop_upp)
                 aware$hypertension_smokeless_prop_upp <- sprintf("%.2f", aware$hypertension_smokeless_prop_upp)
                 aware$hypertension_obese_prop_upp <- sprintf("%.2f", aware$hypertension_obese_prop_upp)
                 aware$hypertension_underweight_prop_upp <- sprintf("%.2f", aware$hypertension_underweight_prop_upp)
                 aware$smoke_smokeless_prop_upp <- sprintf("%.2f", aware$smoke_smokeless_prop_upp)
                 aware$smoke_obese_prop_upp <- sprintf("%.2f", aware$smoke_obese_prop_upp)
                 aware$smoke_underweight_prop_upp <- sprintf("%.2f", aware$smoke_underweight_prop_upp)
                 aware$smokeless_obese_prop_upp <- sprintf("%.2f", aware$smokeless_obese_prop_upp)
                 aware$smokeless_underweight_prop_upp <- sprintf("%.2f", aware$smokeless_underweight_prop_upp)
                 aware$obese_underweight_prop_upp <- sprintf("%.2f", aware$obese_underweight_prop_upp)
                
                



aware <- mutate(aware,
                citempdiabetes_hypertension = str_c(diabetes_hypertension_prop_low, diabetes_hypertension_prop_upp, sep="-"),
                citempdiabetes_smoke = str_c(diabetes_smoke_prop_low, diabetes_smoke_prop_upp, sep="-"),
                citempdiabetes_smokeless = str_c(diabetes_smokeless_prop_low, diabetes_smokeless_prop_upp, sep="-"),
                citempdiabetes_obese = str_c(diabetes_obese_prop_low, diabetes_obese_prop_upp, sep="-"),
                citempdiabetes_underweight = str_c(diabetes_underweight_prop_low, diabetes_underweight_prop_upp, sep="-"),
                citemphypertension_smoke = str_c(hypertension_smoke_prop_low, hypertension_smoke_prop_upp, sep="-"),
                citemphypertension_smokeless = str_c(hypertension_smokeless_prop_low, hypertension_smokeless_prop_upp, sep="-"),
                citemphypertension_obese = str_c(six_risc_prop_low, six_risc_prop_upp, sep="-"),
                citemphypertension_underweight = str_c(hypertension_underweight_prop_low, hypertension_underweight_prop_upp, sep="-"),
                citempsmoke_smokeless = str_c(smoke_smokeless_prop_low, smoke_smokeless_prop_upp, sep="-"),
                citempsmoke_obese = str_c(smoke_obese_prop_low, smoke_obese_prop_upp, sep="-"),
                citempsmoke_underweight = str_c(smoke_underweight_prop_low, smoke_underweight_prop_upp, sep="-"),
                citempsmokeless_obese = str_c(smokeless_obese_prop_low, smokeless_obese_prop_upp, sep="-"),
                citempsmokeless_underweight = str_c(smokeless_underweight_prop_low, smokeless_underweight_prop_upp, sep="-"),
                citempobese_underweight = str_c(obese_underweight_prop_low, obese_underweight_prop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizerocitempdiabetes_hypertension = str_c(bracketstart,  citempdiabetes_hypertension, bracketend, sep=""),
                cionecitempdiabetes_smoke = str_c(bracketstart, citempdiabetes_smoke, bracketend, sep=""),
                citwocitempdiabetes_smokeless = str_c(bracketstart, citempdiabetes_smokeless, bracketend, sep=""),
                cithreecitempdiabetes_obese = str_c(bracketstart, citempdiabetes_obese, bracketend, sep=""),
                cifourcitempdiabetes_underweight = str_c(bracketstart, citempdiabetes_underweight, bracketend, sep=""),
                cifivecitemphypertension_smoke = str_c(bracketstart, citemphypertension_smoke, bracketend, sep=""),
                cisixcitemphypertension_smokeless = str_c(bracketstart, citemphypertension_smokeless, bracketend, sep=""),
                cisixcitemphypertension_obese = str_c(bracketstart, citemphypertension_obese, bracketend, sep=""),
                cisixcitemphypertension_underweight = str_c(bracketstart, citemphypertension_underweight, bracketend, sep=""),
                cisixcitempsmoke_smokeless = str_c(bracketstart, citempsmoke_smokeless, bracketend, sep=""),
                cisixcitempsmoke_obese = str_c(bracketstart, citempsmoke_obese, bracketend, sep=""),
                cisixcitempsmoke_underweight = str_c(bracketstart, citempsmoke_underweight, bracketend, sep=""),
                cisixcitempsmokeless_obese = str_c(bracketstart, citempsmokeless_obese, bracketend, sep=""),
                cisixcitempsmokeless_underweight = str_c(bracketstart, citempsmokeless_underweight, bracketend, sep=""),
                cisixcitempobese_underweight = str_c(bracketstart, citempobese_underweight, bracketend, sep=""),
                rrdiabetes_hypertension_prop = str_c(diabetes_hypertension_prop, cizerocitempdiabetes_hypertension, sep=" "),
                rrdiabetes_smoke_prop = str_c(diabetes_smoke_prop, cionecitempdiabetes_smoke, sep=" "),
                rrdiabetes_smokeless_prop = str_c(diabetes_smokeless_prop, citwocitempdiabetes_smokeless, sep=" "),
                rrdiabetes_obese_prop = str_c(diabetes_obese_prop, cithreecitempdiabetes_obese, sep=" "),
                rrdiabetes_underweight_prop = str_c(diabetes_underweight_prop, cifourcitempdiabetes_underweight, sep=" "),
                rrhypertension_smoke_prop = str_c(hypertension_smoke_prop,  cifivecitemphypertension_smoke, sep=" "),
                rrhypertension_smokeless_prop = str_c(hypertension_smokeless_prop, cisixcitemphypertension_smokeless, sep=" "),
                rrhypertension_obese_prop = str_c(hypertension_obese_prop, cisixcitemphypertension_obese, sep=" "),
                rrhypertension_underweight_prop = str_c(hypertension_underweight_prop, cisixcitemphypertension_underweight, sep=" "),
                rrsmoke_smokeless_prop = str_c(smoke_smokeless_prop, cisixcitempsmoke_smokeless, sep=" "),
                rrsmoke_obese_prop = str_c(smoke_obese_prop, cisixcitempsmoke_obese, sep=" "),
                rrsmoke_underweight_prop = str_c(smoke_underweight_prop, cisixcitempsmoke_underweight, sep=" "),
                rrsmokeless_obese_prop = str_c(smokeless_obese_prop, cisixcitempsmokeless_obese, sep=" "),
                rrsmokeless_underweight_prop = str_c(smokeless_underweight_prop, cisixcitempsmokeless_underweight, sep=" "),
                rrobese_underweight_prop = str_c(obese_underweight_prop, cisixcitempobese_underweight, sep=" "))


write.csv(aware, "unteractions 2way prev risc.csv")

  
  
  
  
  
```

```{r  3 way interactions upsetr ALL RISC FACTORS}
#####3 interactions national

#  svy_all <- merg %>% 
#    as_survey_design(stratum = stratumid,
#                     ids = c(psuid,hh_id),
#                     weights = sworld_weight_india,
#                     variables = c( #Smoking_Hypertension_Diabetes,Smoking_Hypertension_Obese,Smoking_Hypertension_anemia,Smoking_Hypertension_sev_Thinness,
#    Smoking_Diabetes_Obese,
#    Smoking_sev_Thinness_Diabetes,
#    Smoking_Diabetes_sev_Thinness,
#    Obese_Diabetes_Hypertension,
#    Obese_sev_Thinness_Diabetes,
#    Obese_Anemia_Smoking,
#    Obese_Anemia_Hypertension,
#    Anemia_sev_Thinness_Diabetes,
#    Anemia_sev_Thinness_Smoking,
#    Anemia_sev_Thinness_Hypertension,
#    Hypertension_sev_Thinness_Diabetes,
#    Hypertension_Diabetes_sev_Thinness))
  
#  prevtot3 <- svy_all %>%
#    summarize(Smoking_Hypertension_Diabetes_prop = survey_mean(Smoking_Hypertension_Diabetes, proportion=TRUE, vartype = "ci")*100,
#              Smoking_Hypertension_Obese_prop = survey_mean(Smoking_Hypertension_Obese, proportion=TRUE, vartype = "ci")*100 ,
#              Smoking_Hypertension_anemia_prop = survey_mean(Smoking_Hypertension_anemia, proportion=TRUE, vartype = "ci")*100 ,
#              Smoking_Hypertension_sev_Thinness_prop = survey_mean(Smoking_Hypertension_sev_Thinness, proportion=TRUE, vartype = #"ci")*100 ,
#              Smoking_Diabetes_Obese_prop = survey_mean(Smoking_Diabetes_Obese, proportion=TRUE, vartype = "ci")*100 ,
#              Smoking_sev_Thinness_Diabetes_prop = survey_mean(Smoking_sev_Thinness_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
#              Obese_Diabetes_Hypertension_prop = survey_mean(Obese_Diabetes_Hypertension, proportion=TRUE, vartype = "ci")*100 ,
#              Obese_sev_Thinness_Diabetes_prop = survey_mean(Obese_sev_Thinness_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
#              Obese_Anemia_Smoking_prop = survey_mean(Obese_Anemia_Smoking, proportion=TRUE, vartype = "ci")*100 ,
#              Obese_Anemia_Hypertension_prop = survey_mean(Obese_Anemia_Hypertension, proportion=TRUE, vartype = "ci")*100 ,
#              Anemia_sev_Thinness_Diabetes_prop = survey_mean(Anemia_sev_Thinness_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
#                 Anemia_sev_Thinness_Smoking_prop = survey_mean(Anemia_sev_Thinness_Smoking, proportion=TRUE, vartype = "ci")*100 ,
#                 Anemia_sev_Thinness_Hypertension_prop = survey_mean(Anemia_sev_Thinness_Hypertension, proportion=TRUE, vartype = #"ci")*100 ,
#                 Hypertension_sev_Thinness_Diabetes_prop = survey_mean(Hypertension_sev_Thinness_Diabetes, proportion=TRUE, vartype = #"ci")*100)


#write.csv(prevtot3, "prevalence 3way interactions national.csv")



#### 3 way interaction randomized TAKE THIS


     svy_all <- merg %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(  obese_diabetes_hypertension,
   obese_diabetes_smoke,
   obese_diabetes_smokeless,
   obese_hypertension_smoke,
   obese_hypertension_smokeless,
   obese_smoke_smokeless,
   underweight_diabetes_hypertension,
   underweight_diabetes_smoke,
   underweight_diabetes_smokeless,
   underweight_hypertension_smoke,
   underweight_hypertension_smokeless,
   underweight_smoke_smokeless,
   diabetes_hypertension_smoke,
   diabetes_hypertension_smokeless,
   diabetes_smoke_smokeless, 
   hypertension_smoke_smokeless))
     
       prevtot3random <- svy_all %>%
    summarize(obese_diabetes_hypertension_prop = survey_mean(obese_diabetes_hypertension, proportion=TRUE, vartype = "ci")*100,
              obese_diabetes_smoke_prop = survey_mean(obese_diabetes_smoke, proportion=TRUE, vartype = "ci")*100 ,
              obese_diabetes_smokeless_prop = survey_mean(obese_diabetes_smokeless, proportion=TRUE, vartype = "ci")*100 ,
              obese_hypertension_smoke_prop = survey_mean(obese_hypertension_smoke, proportion=TRUE, vartype = "ci")*100 ,
              obese_hypertension_smokeless_prop = survey_mean(obese_hypertension_smokeless, proportion=TRUE, vartype = "ci")*100 ,
              obese_smoke_smokeless_prop = survey_mean(obese_smoke_smokeless, proportion=TRUE, vartype = "ci")*100 ,
              underweight_diabetes_hypertension_prop = survey_mean(underweight_diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              underweight_diabetes_smoke_prop = survey_mean(underweight_diabetes_smoke, proportion=TRUE, vartype = "ci")*100 ,
              underweight_diabetes_smokeless_prop = survey_mean(underweight_diabetes_smokeless, proportion=TRUE, vartype = "ci")*100 ,
              underweight_hypertension_smoke_prop = survey_mean(underweight_hypertension_smoke, proportion=TRUE, vartype = "ci")*100 ,
              underweight_hypertension_smokeless_prop = survey_mean(underweight_hypertension_smokeless, proportion=TRUE, vartype = "ci")*100 ,
                 underweight_smoke_smokeless_prop = survey_mean(underweight_smoke_smokeless, proportion=TRUE, vartype = "ci")*100 ,
                 diabetes_hypertension_smoke_prop = survey_mean(diabetes_hypertension_smoke, proportion=TRUE, vartype = "ci")*100 ,
                 diabetes_hypertension_smokeless_prop = survey_mean(diabetes_hypertension_smokeless, proportion=TRUE, vartype = "ci")*100,
              diabetes_hypertension_smokeless_prop = survey_mean(diabetes_hypertension_smokeless, proportion=TRUE, vartype = "ci")*100,
              diabetes_smoke_smokeless_prop = survey_mean(diabetes_smoke_smokeless, proportion=TRUE, vartype = "ci")*100,
               hypertension_smoke_smokeless_prop = survey_mean( hypertension_smoke_smokeless, proportion=TRUE, vartype = "ci")*100)
              


write.csv(prevtot3random, "prevalence 3way interactions national random.csv")



#######make figure


prevalence.2way.interactions.national <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/prevalence 3way interactions national random.csv")


inter <- prevalence.2way.interactions.national

    heatcols <- hsv(1, 1, seq(1,0,length.out = 16))
  
  Upset <- c('Obese&Diabetes&Hypertension' = (inter$obese_diabetes_hypertension_prop)/100*2320799,
             'Obese&Diabetes&Smoking' = (inter$obese_diabetes_smoke_prop)/100*2320799,
             'Obese&Diabetes&Smokeless tobacco'= (inter$obese_diabetes_smokeless_prop)/100*2320799,
             'Obese&Hypertension&Smoking'= (inter$obese_hypertension_smoke_prop)/100*2320799,
             'Obese&Hypertension&Smokeless tobacco'= (inter$obese_hypertension_smokeless_prop)/100*2320799,
             'Obese&Smoking&Smokeless tobacco'= (inter$obese_smoke_smokeless_prop)/100*2320799,
             'severe Underweight&Diabetes&Hypertension'= (inter$underweight_diabetes_hypertension_prop)/100*2320799,
             'severe Underweight&Diabetes&Smoking'= (inter$underweight_diabetes_smoke_prop)/100*2320799,
             'severe Underweight&Diabetes&Smokeless tobacco'= (inter$underweight_diabetes_smokeless_prop)/100*2320799,
             'severe Underweight&Hypertension&Smoking'= (inter$underweight_hypertension_smoke_prop)/100*2320799,
             'severe Underweight&Hypertension&Smokeless tobacco'= (inter$underweight_hypertension_smokeless_prop)/100*2320799,
             'severe Underweight&Smoking&Smokeless tobacco'= (inter$underweight_smoke_smokeless_prop)/100*2320799,
             'Diabetes&Hypertension&Smoking'= (inter$diabetes_hypertension_smoke_prop)/100*2320799,
             'Diabetes&Hypertension&Smokeless tobacco'= (inter$diabetes_hypertension_smokeless_prop)/100*2320799,
             'Diabetes&Smoking&Smokeless tobacco'= (inter$diabetes_smoke_smokeless_prop)/100*2320799,
             'Hypertension&Smoking&Smokeless tobacco'= (inter$hypertension_smoke_smokeless_prop)/100*2320799)
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals",main.bar.color=heatcols)  


  
  
     
``` 






```{r 2 +3  way interactions upsetr CVD MORB FACTORS}


####2 interactions national

  svy_all4 <- merg %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(diabetes_hypertension,
diabetes_obese,
hypertension_obese,
 obese_diabetes_hypertension))
  
  prevtot2risc4 <- svy_all4 %>%
    summarize(diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100,
              diabetes_obese_prop = survey_mean(diabetes_obese, proportion=TRUE, vartype = "ci")*100 ,
              hypertension_obese_prop = survey_mean(hypertension_obese, proportion=TRUE, vartype = "ci")*100,
              obese_diabetes_hypertension_prop = survey_mean(obese_diabetes_hypertension, proportion=TRUE, vartype = "ci")*100)
  
write.csv(prevtot2risc4, "prevalence 2way interactions national CVD risc only.csv")



prevalence.2way.interactions.national.CVD.risc.only <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/prevalence 2way interactions national CVD risc only.csv")

inter <- prevtot2risc4

mycols <- colors()[c(630, 630, 630, 556)] 


  #  heatcols <- hsv(1, 1, seq(1,0,length.out = 11))
  
  Upset <- c('Diabetes&Hypertension' = (inter$diabetes_hypertension_prop)/100*1275485,
             'Diabetes&Obesity'= (inter$diabetes_obese_prop)/100*1275485,
              'Hypertension&Obesity'= (inter$hypertension_obese_prop)/100*1275485,
             'Obesity&Diabetes&Hypertension'= (inter$obese_diabetes_hypertension_prop)/100*1275485)
 
  
  upset(fromExpression(Upset), order.by = "freq",nsets=3, mainbar.y.label = "Number of Individuals",main.bar.color=mycols)  
  
  
   ######FORMATTING FOR TABLE

#`10.year.age.group.prevalence.multirisc` <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades #with morbidities/prevalence 2way interactions national CVD risc only.csv")
aware <- prevtot2risc4


aware <- mutate(aware,
                diabetes_hypertension_prop = round(diabetes_hypertension_prop,2),
                diabetes_obese_prop = round(diabetes_obese_prop,2),
                hypertension_obese_prop = round(hypertension_obese_prop,2),
                obese_diabetes_hypertension_prop = round(obese_diabetes_hypertension_prop,2),
                  diabetes_hypertension_prop_low = round(diabetes_hypertension_prop_low,2),
                diabetes_obese_prop_low = round(diabetes_obese_prop_low,2),
                hypertension_obese_prop_low = round(hypertension_obese_prop_low,2),
                obese_diabetes_hypertension_prop_low = round(obese_diabetes_hypertension_prop_low,2),
                     diabetes_hypertension_prop_upp = round(diabetes_hypertension_prop_upp,2),
                diabetes_obese_prop_upp = round(diabetes_obese_prop_upp,2),
                obese_diabetes_hypertension_prop_upp = round(obese_diabetes_hypertension_prop_upp,2),
                hypertension_obese_prop_upp = round(hypertension_obese_prop_upp,2))



                
                aware$diabetes_hypertension_prop <- sprintf("%.2f", aware$diabetes_hypertension_prop)
                 aware$diabetes_obese_prop <- sprintf("%.2f", aware$diabetes_obese_prop)
                aware$hypertension_obese_prop <- sprintf("%.2f", aware$hypertension_obese_prop)
                   aware$obese_diabetes_hypertension_prop <- sprintf("%.2f", aware$obese_diabetes_hypertension_prop)
                   aware$diabetes_hypertension_prop_low <- sprintf("%.2f", aware$diabetes_hypertension_prop_low)
                 aware$diabetes_obese_prop_low <- sprintf("%.2f", aware$diabetes_obese_prop_low)
                 aware$hypertension_obese_prop_low <- sprintf("%.2f", aware$hypertension_obese_prop_low)
                  aware$obese_diabetes_hypertension_prop_low <- sprintf("%.2f", aware$obese_diabetes_hypertension_prop_low)
                      aware$diabetes_hypertension_prop_upp <- sprintf("%.2f", aware$diabetes_hypertension_prop_upp)
                 aware$diabetes_obese_prop_upp <- sprintf("%.2f", aware$diabetes_obese_prop_upp)
                 aware$hypertension_obese_prop_upp <- sprintf("%.2f", aware$hypertension_obese_prop_upp)
                  aware$obese_diabetes_hypertension_prop_upp <- sprintf("%.2f", aware$obese_diabetes_hypertension_prop_upp)
                
                



aware <- mutate(aware,
                citempdiabetes_hypertension = str_c(diabetes_hypertension_prop_low, diabetes_hypertension_prop_upp, sep="-"),
                citempdiabetes_obese = str_c(diabetes_obese_prop_low, diabetes_obese_prop_upp, sep="-"),
                citemphypertension_obese = str_c(hypertension_obese_prop_low, hypertension_obese_prop_upp, sep="-"),
                citempobese_diabetes_hypertension = str_c(obese_diabetes_hypertension_prop_low, obese_diabetes_hypertension_prop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizerocitempdiabetes_hypertension = str_c(bracketstart,  citempdiabetes_hypertension, bracketend, sep=""),
                cithreecitempdiabetes_obese = str_c(bracketstart, citempdiabetes_obese, bracketend, sep=""),
                cisixcitemphypertension_obese = str_c(bracketstart, citemphypertension_obese, bracketend, sep=""),
                cisixcitempobese_diabetes_hypertension = str_c(bracketstart, citempobese_diabetes_hypertension, bracketend, sep=""),
                rrdiabetes_hypertension_prop = str_c(diabetes_hypertension_prop, cizerocitempdiabetes_hypertension, sep=" "),
                rrdiabetes_obese_prop = str_c(diabetes_obese_prop, cithreecitempdiabetes_obese, sep=" "),
                 rrobese_diabetes_hypertension_prop = str_c(obese_diabetes_hypertension_prop, cisixcitempobese_diabetes_hypertension, sep=" "),
                rrhypertension_obese_prop = str_c(hypertension_obese_prop, cisixcitemphypertension_obese, sep=" "))


write.csv(aware, "interactions 2way prev CVD risc only.csv")

  
  
  
  
  
```







```{r not used}
     
     

####2 interactions per state



  svy_all <- merg %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(ex_state_ind, Diabetes_Hypertension, Diabetes_Anemia, Obese_Diabetes, Smoking_Diabetes, sev_Thinness_Diabetes, Hypertension_Anemia, Obese_Hypertension, sev_Thinness_Hypertension, Smoking_Hypertension, Obese_Anemia, Obese_Smoking, Smoking_sev_Thinness, Anemia_sev_Thinness, Anemia_Smoking))
  
  prevtot <- svy_all %>%
    group_by(ex_state_ind) %>%
    summarize(Diabetes_Hypertension_prop = survey_mean(Diabetes_Hypertension, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Anemia_prop = survey_mean(Diabetes_Anemia, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Diabetes_prop = survey_mean(Obese_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
              Smoking_Diabetes_prop = survey_mean(Smoking_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
              sev_Thinness_Diabetes_prop = survey_mean(sev_Thinness_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
              Hypertension_Anemia_prop = survey_mean(Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Hypertension_prop = survey_mean(Obese_Hypertension, proportion=TRUE, vartype = "ci")*100 ,
              sev_Thinness_Hypertension_prop = survey_mean(sev_Thinness_Hypertension, proportion=TRUE, vartype = "ci")*100 ,
              Smoking_Hypertension_prop = survey_mean(Smoking_Hypertension, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Anemia_prop = survey_mean(Obese_Anemia, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Smoking_prop = survey_mean(Obese_Smoking, proportion=TRUE, vartype = "ci")*100 ,
                 Smoking_sev_Thinness_prop = survey_mean(Smoking_sev_Thinness, proportion=TRUE, vartype = "ci")*100 ,
                 Anemia_sev_Thinness_prop = survey_mean(Anemia_sev_Thinness, proportion=TRUE, vartype = "ci")*100 ,
                 Anemia_Smoking_prop = survey_mean(Anemia_Smoking, proportion=TRUE, vartype = "ci")*100)
          
  ######2 way interactions by wealth quintile            

  svy_all <- merg %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(wealth_quintile_rurb_lab, Diabetes_Hypertension, Diabetes_Anemia, Obese_Diabetes, Smoking_Diabetes, sev_Thinness_Diabetes, Hypertension_Anemia, Obese_Hypertension, sev_Thinness_Hypertension, Smoking_Hypertension, Obese_Anemia, Obese_Smoking, Smoking_sev_Thinness, Anemia_sev_Thinness, Anemia_Smoking))
  
  prevtot <- svy_all %>%
    group_by(wealth_quintile_rurb_lab) %>%
    summarize(Diabetes_Hypertension_prop = survey_mean(Diabetes_Hypertension, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Anemia_prop = survey_mean(Diabetes_Anemia, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Diabetes_prop = survey_mean(Obese_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
              Smoking_Diabetes_prop = survey_mean(Smoking_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
              sev_Thinness_Diabetes_prop = survey_mean(sev_Thinness_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
              Hypertension_Anemia_prop = survey_mean(Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Hypertension_prop = survey_mean(Obese_Hypertension, proportion=TRUE, vartype = "ci")*100 ,
              sev_Thinness_Hypertension_prop = survey_mean(sev_Thinness_Hypertension, proportion=TRUE, vartype = "ci")*100 ,
              Smoking_Hypertension_prop = survey_mean(Smoking_Hypertension, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Anemia_prop = survey_mean(Obese_Anemia, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Smoking_prop = survey_mean(Obese_Smoking, proportion=TRUE, vartype = "ci")*100 ,
                 Smoking_sev_Thinness_prop = survey_mean(Smoking_sev_Thinness, proportion=TRUE, vartype = "ci")*100 ,
                 Anemia_sev_Thinness_prop = survey_mean(Anemia_sev_Thinness, proportion=TRUE, vartype = "ci")*100 ,
                 Anemia_Smoking_prop = survey_mean(Anemia_Smoking, proportion=TRUE, vartype = "ci")*100)
          
  
  
```





```{r Euler diagrams not used}





##### Venn diagrams


length(which(merg$ex_diab_narrow_ind==1))
length(which(merg$ex_htn_narrow_ind==1))
length(which(merg$ex_anemia_ind==1))
length(which(merg$obese==1))
length(which(merg$overweight==1))
length(which(merg$underweight==1))
length(which(merg$smoke==1))

length(which(merg$overweight==1& merg$ex_htn_narrow_ind==1))

(length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1)))

(length(which(merg$ex_diab_narrow_ind==1& merg$ex_anemia_ind==1)))
(length(which(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1)))

(length(which(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1 & merg$ex_diab_narrow_ind==1)))


merg <- mutate(merg, 
               overweight_htn = ifelse( overweight==1 & ex_htn_narrow_ind== 1, 1,0))

merg$overweight_htn <- as.factor(merg$overweight_htn)
  summary(merg$overweight_htn)
  
######FOUR SETS  
  
  
  fit2 <- euler(c(Diabetes = (length(which(merg$ex_diab_narrow_ind==1))), Hypertension = (length(which(merg$ex_htn_narrow_ind==1))), Anemia = (length(which(merg$ex_anemia_ind==1))), Overweight = (length(which(merg$overweight==1))),
                  "Diabetes&Hypertension" = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1))),
                  "Diabetes&Anemia" = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_anemia_ind==1))),
                  "Hypertension&Anemia"= (length(which(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1))) ,
                  "Overweight&Diabetes"= (length(which(merg$ex_diab_narrow_ind==1& merg$overweight==1))),
                  "Overweight&Hypertension"= (length(which(merg$ex_htn_narrow_ind==1& merg$overweight==1))),
                  "Overweight&Anemia"= (length(which(merg$ex_anemia_ind==1& merg$overweight==1))),
                  "Diabetes&Hypertension&Overweight" = (length(which(merg$ex_htn_narrow_ind==1& merg$overweight==1 & merg$ex_diab_narrow_ind==1))),
                "Diabetes&Anemia&Overweight" = (length(which(merg$ex_anemia_ind==1& merg$overweight==1 & merg$ex_diab_narrow_ind==1))),
                "Diabetes&Anemia&Overweight&Hypertension" = (length(which(merg$ex_anemia_ind==1& merg$overweight==1 & merg$ex_diab_narrow_ind==1 & merg$ex_htn_narrow_ind==1))),
                  "Diabetes&Hypertension&Anemia" = (length(which(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1 & merg$ex_diab_narrow_ind==1)))), shape =  "ellipse")
  plot(fit2,
       fills = c("dodgerblue4", "darkgoldenrod1", "cornsilk4", "firebrick"),
       edges = TRUE,
       fontsize = 8,
       quantities = list(fontsize = 8))
  
  ######FOUR CVD RISK SETS  
  
  
  fit2 <- euler(c(Diabetes = (length(which(merg$ex_diab_narrow_ind==1))), Hypertension = (length(which(merg$ex_htn_narrow_ind==1))), Smoking = (length(which(merg$smoke==1))), Obese = (length(which(merg$obese==1))),
                  "Diabetes&Hypertension" = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1))),
                  "Diabetes&Smoking" = (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1))),
                  "Hypertension&Smoking"= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1))) ,
                  "Obese&Diabetes"= (length(which(merg$ex_diab_narrow_ind==1& merg$obese==1))),
                  "Obese&Hypertension"= (length(which(merg$ex_htn_narrow_ind==1& merg$obese==1))),
                  "Obese&Smoking"= (length(which(merg$smoke==1& merg$obese==1))),
                  "Diabetes&Hypertension&Obese" = (length(which(merg$ex_htn_narrow_ind==1& merg$obese==1 & merg$ex_diab_narrow_ind==1))),
                "Diabetes&Smoking&Obese" = (length(which(merg$smoke==1& merg$obese==1 & merg$ex_diab_narrow_ind==1))),
                "Diabetes&Smoking&Obese&Hypertension" = (length(which(merg$smoke==1& merg$obese==1 & merg$ex_diab_narrow_ind==1 & merg$ex_htn_narrow_ind==1))),
                  "Diabetes&Hypertension&Smoking" = (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$ex_diab_narrow_ind==1))), "Obese&Hypertension&Smoking" = (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$obese==1)))), shape =  "ellipse")
  plot(fit2,
       fills = c("dodgerblue4", "darkgoldenrod1", "cornsilk4", "firebrick"),
       edges = TRUE,
       fontsize = 8,
       quantities = list(fontsize = 8))
  fit2
  
  ######ANEMIA INTERACTIONS
  
    fit2 <- euler(c(Diabetes = (length(which(merg$ex_diab_narrow_ind==1))), Hypertension = (length(which(merg$ex_htn_narrow_ind==1))), Smoking = (length(which(merg$smoke==1))), Obese = (length(which(merg$obese==1))), Anemia = (length(which(merg$ex_anemia_ind==1))), sev_Thinness = (length(which(merg$sev_underweight==1))),
                  'Anemia&sev_Thinness'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(merg$smoke==1& merg$ex_anemia_ind==1))),
   'Obese&Anemia'= (length(which(merg$ex_anemia_ind==1& merg$obese==1))),
    'Hypertension&Anemia'= (length(which(merg_men$ex_htn_narrow_ind==1& merg_men$ex_anemia_ind==1))),
    'Diabetes&Anemia' = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_anemia_ind==1)))),shape= "ellipse")
  plot(fit2,
       fills = c("dodgerblue4", "darkgoldenrod1", "cornsilk4", "firebrick","burlywood","darkgreen" ),
       edges = TRUE,
       fontsize = 8,
       quantities = list(fontsize = 8))
  
   fit2
  

  
  
#####THREE SETS
  
  fit4 <- euler(c(Diabetes = (length(which(merg$ex_diab_narrow_ind==1))), Hypertension = (length(which(merg$ex_htn_narrow_ind==1))), Anemia = (length(which(merg$ex_anemia_ind==1))), 
                  "Diabetes&Hypertension" = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1))),
                  "Diabetes&Anemia" = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_anemia_ind==1))),
                  "Hypertension&Anemia"= (length(which(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1))) ,
                 "Diabetes&Hypertension&Anemia" = (length(which(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1 & merg$ex_diab_narrow_ind==1)))), shape= "ellipse")
  plot(fit3,
       fills = c("dodgerblue4", "darkgoldenrod1", "cornsilk4"),
       edges = TRUE,
       fontsize = 8,
       quantities = list(fontsize = 8))
  fit3
  
  
  ########Three sets diabetes hypertension overweight
  
  fit4 <- euler(c(Diabetes = (length(which(merg$ex_diab_narrow_ind==1))), Hypertension = (length(which(merg$ex_htn_narrow_ind==1))), Overweight = (length(which(merg$overweight==1))), 
                  "Diabetes&Hypertension" = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1))),
                  "Overweight&Diabetes"= (length(which(merg$ex_diab_narrow_ind==1& merg$overweight==1))),
                  "Overweight&Hypertension"= (length(which(merg$ex_htn_narrow_ind==1& merg$overweight==1))),
                  "Diabetes&Hypertension&Overweight" = (length(which(merg$ex_htn_narrow_ind==1 & merg$overweight==1 & merg$ex_diab_narrow_ind==1)))), shape= "ellipse")
  plot(fit4,
       fills = c("dodgerblue4", "darkgoldenrod1", "cornsilk4"),
       edges = TRUE,
       fontsize = 8,
       quantities = list(fontsize = 8))
  fit4
  
  ##########Four sets diabetes and bmi variables
  
  fit4 <- euler(c(Diabetes = (length(which(merg$ex_diab_narrow_ind==1))), sev_underweight = (length(which(merg$sev_underweight==1))), Overweight = (length(which(merg$overweight==1))), Obese = (length(which(merg$obese==1))),
                  "Diabetes&Hypertension" = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1))),
                  "Diabetes&Obese"= (length(which(merg$ex_diab_narrow_ind==1& merg$obese==1))),
                  "Diabetes&sev_Thinness"= (length(which(merg$ex_diab_narrow_ind==1& merg$sev_underweight==1)))),
                
                   shape= "ellipse")
  plot(fit4,
       fills = c("dodgerblue4", "darkgoldenrod1", "cornsilk4", "firebrick"),
       edges = TRUE,
       fontsize = 8,
       quantities = list(fontsize = 8))

  fit4
  
  #########Hypertension and bmi variables
  
  ##########Four sets diabetes and bmi variables
  
  fit4 <- euler(c(Hypertension = (length(which(merg$ex_htn_narrow_ind==1))), Severe-thinness = (length(which(merg$sev_underweight==1))), Overweight = (length(which(merg$overweight==1))), Obese = (length(which(merg$obese==1))),
                  "Hypertension&Overweight" = (length(which(merg$ex_htn_narrow_ind==1& merg$overweight==1))),
                  "Hypertension&Obese"= (length(which(merg$ex_htn_narrow_ind==1& merg$obese==1))),
                  "Hypertension&Severe-thinness"= (length(which(merg$ex_htn_narrow_ind==1& merg$sev_underweight==1)))),
                shape= "ellipse")
  plot(fit4,
       fills = c("dodgerblue4", "darkgoldenrod1", "cornsilk4", "firebrick"),
       edges = TRUE,
       fontsize = 8,
       quantities = list(fontsize = 8))
  
```



```{r UPSET 2 interactions not used}
  
  
  ######Upsetr mit allen krankheiten aus merg, nur 2 interactions
  
  heatcols <- hsv(1, 1, seq(1,0,length.out = 14))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1))),
             'Diabetes&Anemia' = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_anemia_ind==1))),
             'Obese&Diabetes'= (length(which(merg$ex_diab_narrow_ind==1& merg$obese==1))),
             'Smoking&Diabetes'= (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1))),
             'sev_Thinness&Diabetes'= (length(which(merg$ex_diab_narrow_ind==1& merg$sev_underweight==1))),
             'Hypertension&Anemia'= (length(which(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1))) ,
             'Obese&Hypertension'= (length(which(merg$ex_htn_narrow_ind==1& merg$obese==1))),
             'sev_Thinness&Hypertension'= (length(which(merg$ex_htn_narrow_ind==1& merg$sev_underweight==1))),
             'Smoking&Hypertension'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1))),
             'Obese&Anemia'= (length(which(merg$ex_anemia_ind==1& merg$obese==1))),
             'Obese&Smoking'= (length(which(merg$smoke==1& merg$obese==1))),
             'Smoking&sev_Thinness'= (length(which(merg$sev_underweight==1& merg$smoke==1))),
             'Anemia&sev_Thinness'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(merg$smoke==1& merg$ex_anemia_ind==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
  #####Group by sets
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, group.by= "sets", mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)
  
  
  ######MEN 2 interactions
  
    heatcols <- hsv(1, 1, seq(1,0,length.out = 14))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(merg_men$ex_diab_narrow_ind==1& merg_men$ex_htn_narrow_ind==1))),
             'Diabetes&Anemia' = (length(which(merg_men$ex_diab_narrow_ind==1& merg_men$ex_anemia_ind==1))),
             'Obese&Diabetes'= (length(which(merg_men$ex_diab_narrow_ind==1& merg_men$obese==1))),
             'Smoking&Diabetes'= (length(which(merg_men$ex_diab_narrow_ind==1& merg_men$smoke==1))),
             'sev_Thinness&Diabetes'= (length(which(merg_men$ex_diab_narrow_ind==1& merg_men$sev_underweight==1))),
             'Hypertension&Anemia'= (length(which(merg_men$ex_htn_narrow_ind==1& merg_men$ex_anemia_ind==1))) ,
             'Obese&Hypertension'= (length(which(merg_men$ex_htn_narrow_ind==1& merg_men$obese==1))),
             'sev_Thinness&Hypertension'= (length(which(merg_men$ex_htn_narrow_ind==1& merg_men$sev_underweight==1))),
             'Smoking&Hypertension'= (length(which(merg_men$ex_htn_narrow_ind==1& merg_men$smoke==1))),
             'Obese&Anemia'= (length(which(merg_men$ex_anemia_ind==1& merg_men$obese==1))),
             'Obese&Smoking'= (length(which(merg_men$smoke==1& merg_men$obese==1))),
             'Smoking&sev_Thinness'= (length(which(merg_men$sev_underweight==1& merg_men$smoke==1))),
             'Anemia&sev_Thinness'= (length(which(merg_men$sev_underweight==1& merg_men$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(merg_men$smoke==1& merg_men$ex_anemia_ind==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
  ####WOMEN 2 interactions
  
      heatcols <- hsv(1, 1, seq(1,0,length.out = 14))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(merg_women$ex_diab_narrow_ind==1& merg_women$ex_htn_narrow_ind==1))),
             'Diabetes&Anemia' = (length(which(merg_women$ex_diab_narrow_ind==1& merg_women$ex_anemia_ind==1))),
             'Obese&Diabetes'= (length(which(merg_women$ex_diab_narrow_ind==1& merg_women$obese==1))),
             'Smoking&Diabetes'= (length(which(merg_women$ex_diab_narrow_ind==1& merg_women$smoke==1))),
             'sev_Thinness&Diabetes'= (length(which(merg_women$ex_diab_narrow_ind==1& merg_women$sev_underweight==1))),
             'Hypertension&Anemia'= (length(which(merg_women$ex_htn_narrow_ind==1& merg_women$ex_anemia_ind==1))) ,
             'Obese&Hypertension'= (length(which(merg_women$ex_htn_narrow_ind==1& merg_women$obese==1))),
             'sev_Thinness&Hypertension'= (length(which(merg_women$ex_htn_narrow_ind==1& merg_women$sev_underweight==1))),
             'Smoking&Hypertension'= (length(which(merg_women$ex_htn_narrow_ind==1& merg_women$smoke==1))),
             'Obese&Anemia'= (length(which(merg_women$ex_anemia_ind==1& merg_women$obese==1))),
             'Obese&Smoking'= (length(which(merg_women$smoke==1& merg_women$obese==1))),
             'Smoking&sev_Thinness'= (length(which(merg_women$sev_underweight==1& merg_women$smoke==1))),
             'Anemia&sev_Thinness'= (length(which(merg_women$sev_underweight==1& merg_women$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(merg_women$smoke==1& merg_women$ex_anemia_ind==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
  
```




  



```{r 2+3 interactions not used}
  
  ######## all 2+ 3 interactions
  
  heatcols <- hsv(1, 1, seq(1,0,length.out = 29))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1))),
             'Diabetes&Anemia' = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_anemia_ind==1))),
             'Obese&Diabetes'= (length(which(merg$ex_diab_narrow_ind==1& merg$obese==1))),
             'Smoking&Diabetes'= (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1))),
             'sev_Thinness&Diabetes'= (length(which(merg$ex_diab_narrow_ind==1& merg$sev_underweight==1))),
             'Hypertension&Anemia'= (length(which(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1))) ,
             'Obese&Hypertension'= (length(which(merg$ex_htn_narrow_ind==1& merg$obese==1))),
             'sev_Thinness&Hypertension'= (length(which(merg$ex_htn_narrow_ind==1& merg$sev_underweight==1))),
             'Smoking&Hypertension'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1))),
             'Obese&Anemia'= (length(which(merg$ex_anemia_ind==1& merg$obese==1))),
             'Obese&Smoking'= (length(which(merg$smoke==1& merg$obese==1))),
             'Smoking&sev_Thinness'= (length(which(merg$sev_underweight==1& merg$smoke==1))),
             'Anemia&sev_Thinness'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(merg$smoke==1& merg$ex_anemia_ind==1))),
             'Smoking&Hypertension&Diabetes'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$ex_diab_narrow_ind==1))),
             'Smoking&Hypertension&Obese'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$obese==1))),
             'Smoking&Hypertension&anemia'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$ex_anemia_ind==1))),
             'Smoking&Hypertension&sev_Thinness'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$sev_underweight==1))),
             'Smoking&Diabetes&Obese'= (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$obese==1))),
             'Smoking&Diabetes&Anemia'= (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$ex_anemia_ind==1))),
             'Smoking&Diabetes&sev_Thinness'= (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$sev_underweight==1))),
             'Obese&Diabetes&Hypertension'= (length(which(merg$ex_diab_narrow_ind==1& merg$obese==1 & merg$ex_htn_narrow_ind==1))),
             'Obese&Diabetes&Anemia'= (length(which(merg$ex_diab_narrow_ind==1& merg$obese==1 & merg$ex_anemia_ind==1))),
             'Obese&Anemia&Smoking'= (length(which(merg$ex_anemia_ind==1& merg$obese==1 & merg$smoke==1))),
             'Obese&Anemia&Hypertension'= (length(which(merg$ex_anemia_ind==1& merg$obese==1 & merg$ex_htn_narrow_ind==1))),
             'Anemia&sev_Thinness&Diabetes'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$ex_diab_narrow_ind==1))),
             'Anemia&sev_Thinness&Smoking'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$smoke==1))),
             'Anemia&sev_Thinness&Hypertension'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$ex_htn_narrow_ind==1))),
             'Hypertension&Diabetes&Anemia'= (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1 & merg$ex_anemia_ind==1))),
             'Hypertension&Diabetes&sev_Thinness'= (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1 & merg$sev_underweight==1))))
             
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, main.bar.color=heatcols)
  
  #####all 3 only interactions

heatcols <- hsv(1, 1, seq(1,0,length.out = 16))

Upset <- c('Smoking&Hypertension&Diabetes'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$ex_diab_narrow_ind==1))),
           'Smoking&Hypertension&Obese'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$obese==1))),
           'Smoking&Hypertension&Anemia'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$ex_anemia_ind==1))),
           'Smoking&Hypertension&sev_Thinness'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$sev_underweight==1))),
           'Smoking&Diabetes&Obese'= (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$obese==1))),
           'Smoking&Diabetes&Anemia'= (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$ex_anemia_ind==1))),
           'Smoking&Diabetes&sev_Thinness'= (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$sev_underweight==1))),
           'Obese&Diabetes&Hypertension'= (length(which(merg$ex_diab_narrow_ind==1& merg$obese==1 & merg$ex_htn_narrow_ind==1))),
           'Obese&Diabetes&Anemia'= (length(which(merg$ex_diab_narrow_ind==1& merg$obese==1 & merg$ex_anemia_ind==1))),
           'Obese&Anemia&Smoking'= (length(which(merg$ex_anemia_ind==1& merg$obese==1 & merg$smoke==1))),
           'Obese&Anemia&Hypertension'= (length(which(merg$ex_anemia_ind==1& merg$obese==1 & merg$ex_htn_narrow_ind==1))),
           'Anemia&sev_Thinness&Diabetes'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$ex_diab_narrow_ind==1))),
           'Anemia&sev_Thinness&Smoking'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$smoke==1))),
           'Anemia&sev_Thinness&Hypertension'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$ex_htn_narrow_ind==1))),
           'Hypertension&Diabetes&Anemia'= (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1 & merg$ex_anemia_ind==1))),
           'Hypertension&Diabetes&sev_Thinness'= (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1 & merg$sev_underweight==1))))


upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     

######Goup by sets  
upset(fromExpression(Upset), order.by = "freq",nsets=6,group.by= "sets", mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     

  
  ###########As percentages
  
  
  
  Upset <- c('Diabetes&Hypertension' = (((length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1)))/2320551)*100),
             'Diabetes&Anemia' = (((length(which(merg$ex_diab_narrow_ind==1& merg$ex_anemia_ind==1)))/2320551)*100),
             'Obese&Diabetes'= (((length(which(merg$ex_diab_narrow_ind==1& merg$obese==1)))/2320551)*100),
             'Smoking&Diabetes'= (((length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1)))/2320551)*100),
             'sev_Thinness&Diabetes'= (((length(which(merg$ex_diab_narrow_ind==1& merg$sev_underweight==1)))/2320551)*100),
             'Hypertension&Anemia'= (((length(which(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1)))/2320551)*100),
             'Obese&Hypertension'= (((length(which(merg$ex_htn_narrow_ind==1& merg$obese==1)))/2320551)*100),
             'sev_Thinness&Hypertension'= (((length(which(merg$ex_htn_narrow_ind==1& merg$sev_underweight==1)))/2320551)*100),
             'Smoking&Hypertension'= (((length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1)))/2320551)*100),
             'Obese&Anemia'= (((length(which(merg$ex_anemia_ind==1& merg$obese==1)))/2320551)*100),
             'Obese&Smoking'= (((length(which(merg$smoke==1& merg$obese==1)))/2320551)*100),
             'Smoking&sev_Thinness'= (((length(which(merg$sev_underweight==1& merg$smoke==1)))/2320551)*100),
             'Anemia&sev_Thinness'= (((length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1)))/2320551)*100),
             'Anemia&Smoking'= (((length(which(merg$smoke==1& merg$ex_anemia_ind==1)))/2320551)*100))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals")     
  
  
  
  
  
  
  Upset <- c('Diabetes&Hypertension' = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1))),
             'Diabetes&Anemia' = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_anemia_ind==1))),
             'obese&Diabetes'= (length(which(merg$ex_diab_narrow_ind==1& merg$obese==1))),
             'Smoking&Diabetes'= (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1))),
             'Hypertension&Anemia'= (length(which(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1))),
             'obese&Hypertension'= (length(which(merg$ex_htn_narrow_ind==1& merg$obese==1))),
             'Smoking&Hypertension'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1))),
             'obese&Anemia'= (length(which(merg$ex_anemia_ind==1& merg$obese==1))),
             'obese&Smoking'= (length(which(merg$smoke==1& merg$obese==1))),
             'Anemia&Smoking'= (length(which(merg$smoke==1& merg$ex_anemia_ind==1)))),
                 'Smoking&Thinness'= (length(which(merg$sev_underweight==1& merg$smoke==1)))
               'Anemia&Thinness'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq", mainbar.y.label = "Number of Individuals")       
  
             
             
             
             'Diabetes&Hypertension&Severe_thinness' = (length(which(merg$ex_htn_narrow_ind==1& merg$sev_underweight==1 & merg$ex_diab_narrow_ind==1))),
             'Diabetes&Anemia&Severe_thinness' = (length(which(merg$ex_anemia_ind==1& merg$sev_underweight==1 & merg$ex_diab_narrow_ind==1))),
             'Diabetes&Anemia&Severe_thinness&Hypertension' = (length(which(merg$ex_anemia_ind==1& merg$sev_underweight==1 & merg$ex_diab_narrow_ind==1 & merg$ex_htn_narrow_ind==1))),
             
           
             
             'Diabetes&Hypertension&Smoking' = (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$ex_diab_narrow_ind==1))),
             'Diabetes&Anemia&Smoking' = (length(which(merg$ex_anemia_ind==1& merg$smoke==1 & merg$ex_diab_narrow_ind==1))),
             'Diabetes&Anemia&Smoking&Hypertension' = (length(which(merg$ex_anemia_ind==1& merg$smoke==1 & merg$ex_diab_narrow_ind==1 & merg$ex_htn_narrow_ind==1))),
             'Diabetes&Hypertension&obese' = (length(which(merg$ex_htn_narrow_ind==1& merg$obese==1 & merg$ex_diab_narrow_ind==1))),
             'Diabetes&Anemia&obese' = (length(which(merg$ex_anemia_ind==1& merg$obese==1 & merg$ex_diab_narrow_ind==1))),
             'Diabetes&Anemia&obese&Hypertension' = (length(which(merg$ex_anemia_ind==1& merg$obese==1 & merg$ex_diab_narrow_ind==1 & merg$ex_htn_narrow_ind==1))),
             'Diabetes&Hypertension&Anemia' = (length(which(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1 & merg$ex_diab_narrow_ind==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq", mainbar.y.label = "Number of Individuals")
  
  merg <- mutate(merg, 
                 diab_htn = ifelse( ex_diab_narrow_ind==1 & ex_htn_narrow_ind== 1, 1,0))
  
  merg$diab_htn <- as.factor(merg$diab_htn)
  summary(merg$diab_htn)
  
```




```{r CVD risc diagram not used}

######CVD Risk 
  
  
  
  heatcols <- hsv(1, 1, seq(1,0,length.out = 11))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1))),
             'Obese&Diabetes'= (length(which(merg$ex_diab_narrow_ind==1& merg$obese==1))),
             'Smoking&Diabetes'= (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1))),
             'Obese&Hypertension'= (length(which(merg$ex_htn_narrow_ind==1& merg$obese==1))),
             'Smoking&Hypertension'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1))),
             'Obese&Smoking'= (length(which(merg$smoke==1& merg$obese==1))),
             'Smoking&Hypertension&Diabetes'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$ex_diab_narrow_ind==1))),
             'Smoking&Hypertension&Obese'= (length(which(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$obese==1))),
             'Smoking&Diabetes&Obese'= (length(which(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$obese==1))),
             'Obese&Diabetes&Hypertension'= (length(which(merg$ex_diab_narrow_ind==1& merg$obese==1 & merg$ex_htn_narrow_ind==1))),
             'Diabetes&Hypertension&Obese&Smoking' = (length(which(merg$ex_diab_narrow_ind==1 & merg$ex_htn_narrow_ind==1 & merg$obese==1 & merg$smoke==1))))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=4, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)      


```

 

```{r 2way interaction population estimates not used}
     
    

  heatcols <- hsv(1, 1, seq(1,0,length.out = 9))
  
  Upset <- c('Diabetes&Hypertension' = mean(prevtot$Diabetes_Hypertension_prop),
             'Diabetes&Anemia' = mean(prevtot$Diabetes_Anemia_prop),
             'Obese&Diabetes'= mean(prevtot$Obese_Diabetes_prop),
             'Smoking&Diabetes'= mean(prevtot$Smoking_Diabetes_prop),
             'sev_Thinness&Diabetes'= mean(prevtot$sev_Thinness_Diabetes_prop),
             'Hypertension&Anemia'= mean(prevtot$Hypertension_Anemia_prop),
             'Obese&Hypertension'= mean(prevtot$Obese_Hypertension_prop),
             'sev_Thinness&Hypertension'= mean(prevtot$sev_Thinness_Hypertension_prop),
             'Smoking&Hypertension'= mean(prevtot$Smoking_Hypertension_prop),
             'Obese&Anemia'= mean(prevtot$Obese_Anemia_prop),
             'Obese&Smoking'= mean(prevtot$Obese_Smoking_prop),
             'Smoking&sev_Thinness'= mean(prevtot$Smoking_sev_Thinness_prop),
             'Anemia&sev_Thinness'= mean(prevtot$Anemia_sev_Thinness_prop),
             'Anemia&Smoking'= mean(prevtot$Anemia_Smoking_prop))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  

  

      Diabetes_Hypertension = ifelse(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1,1,0),
    sev_Thinness_Diabetes = ifelse(merg$ex_diab_narrow_ind==1& merg$ex_anemia_ind==1,1,0),
    Obese_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$obese==1,1,0),
    Smoking_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$smoke==1,1,0),
    sev_Thinness_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$sev_underweight==1,1,0),
    Hypertension_Anemia= ifelse(merg$ex_htn_narrow_ind==1& merg$ex_anemia_ind==1,1,0),
    Obese_Hypertension= ifelse(merg$ex_htn_narrow_ind==1& merg$obese==1,1,0),
    sev_Thinness_Hypertension= ifelse(merg$ex_htn_narrow_ind==1& merg$sev_underweight==1,1,0),
    Smoking_Hypertension= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1,1,0),
    Obese_Anemia= ifelse(merg$ex_anemia_ind==1& merg$obese==1,1,0),
    Obese_Smoking= ifelse(merg$smoke==1& merg$obese==1,1,0),
    Smoking_sev_Thinness= ifelse(merg$sev_underweight==1& merg$smoke==1,1,0),
    Anemia_sev_Thinness= ifelse(merg$sev_underweight==1& merg$ex_anemia_ind==1,1,0),
    Anemia_Smoking= ifelse(merg$smoke==1& merg$ex_anemia_ind==1,1,0),
    Smoking_Hypertension_Diabetes= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$ex_diab_narrow_ind==1,1,0),
    Smoking_Hypertension_Obese= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$obese==1,1,0),
    Smoking_Hypertension_anemia= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$ex_anemia_ind==1,1,0),
    Smoking_Hypertension_sev_Thinness= ifelse(merg$ex_htn_narrow_ind==1& merg$smoke==1 & merg$sev_underweight==1,1,0),
    Smoking_Diabetes_Obese= ifelse(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$obese==1,1,0),
    Smoking_sev_Thinness_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$ex_anemia_ind==1,1,0),
    Smoking_Diabetes_sev_Thinness= ifelse(merg$ex_diab_narrow_ind==1& merg$smoke==1 & merg$sev_underweight==1,1,0),
    Obese_Diabetes_Hypertension= ifelse(merg$ex_diab_narrow_ind==1& merg$obese==1 & merg$ex_htn_narrow_ind==1,1,0),
    Obese_sev_Thinness_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$obese==1 & merg$ex_anemia_ind==1,1,0),
    Obese_Anemia_Smoking= ifelse(merg$ex_anemia_ind==1& merg$obese==1 & merg$smoke==1,1,0),
    Obese_Anemia_Hypertension= ifelse(merg$ex_anemia_ind==1& merg$obese==1 & merg$ex_htn_narrow_ind==1,1,0),
    Anemia_sev_Thinness_Diabetes= ifelse(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$ex_diab_narrow_ind==1,1,0),
    Anemia_sev_Thinness_Smoking= ifelse(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$smoke==1,1,0),
    Anemia_sev_Thinness_Hypertension= ifelse(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$ex_htn_narrow_ind==1,1,0),
    Hypertension_sev_Thinness_Diabetes= ifelse(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1 & merg$ex_anemia_ind==1,1,0),
    Hypertension_Diabetes_sev_Thinness= ifelse(merg$ex_diab_narrow_ind==1& merg$ex_htn_narrow_ind==1 & merg$sev_underweight==1,1,0))
    
  write_csv(prevtot, "prev interaction terms.csv")
  
  
    svy_all <- merg %>% 
    as_survey_design(stratum = stratum,
                     ids = c(p_id),
                     weights = sweight_merge,
                     variables = c(Obese_Hypertension))
  
  prevtot <- svy_all %>%
    summarize(obese_hyp = survey_mean(Obese_Hypertension, proportion=TRUE, vartype = "ci"))


  write_csv(prevtot, "prev Obese_Hypertension.csv")
  
  
####Upsetr with survey design values
  
  
heatcols <- hsv(1, 1, seq(1,0,length.out = 2))
  
  Upset <- c('Diabetes&Hypertension' = (mean(prevtot$diab_hyp)),
             'Diabetes&Anemia' = (mean(prevtot$diab_anem)),
             'Obese&Diabetes'= (mean(prevtot$diab_obese)))
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=3, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)  
  
```
  
  
```{r CUT DHS ANALYSIS FROM HERE}  
``` 
  



```{r Output DHS DATASET}

```



```{r MAP Multimorbidity}

#  SET-UP  #


# http://stackoverflow.com/questions/28322866/mapping-just-one-state-of-india-and-writing-its-name-inside-the-state-boundary
library(rgeos)
library(rgdal)
library(raster) # get data for maps

india <- getData("GADM", country = "India", level = 1)
dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1)
india <- gSimplify(india, tol=0.01, topologyPreserve=TRUE) # this drastically reduces the detail in the GADM file to allow for decently quick plotting
map <- fortify(india) # makes a dataset out of a spatial object
map$id <- as.integer(map$id) # that's just to be able to dhs_nomisse it

#dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1) # This is just a df of state names and state IDs
dat <- filter(dat, 
              row_number() != 31) # This just removes the Tamil Nadu duplicate (for some reason the india spatial data has a separate row for Madras as for TN)


centers <- data.frame(gCentroid(india, byid = TRUE)) # a df of latitude and longitude
centers <- filter(centers, 
                  row_number() !=31)  # This is removing the Tamil Nadu duplicate
centers$state <- as.factor(dat$state)  # adding state names to it
centers <- as_tibble(centers)

# Abbreviating the state names and throwing out Lakshadweep and Dadra, Nagar Haveli, D&D, Chandigarh, and Puducherry

centers <- centers %>% 
  mutate(state = fct_recode(state, 
                                     "HP" = "Himachal Pradesh",
                                     "PB" = "Punjab",
                                     "Chandigarh" = "Chandigarh",
                                     "HR" = "Haryana",
                                     "DL" = "NCT of Delhi",
                                     "SK" = "Sikkim",
                                     "Daman and Diu" = "Daman and Diu",
                                     "AR" = "Arunachal Pradesh",
                                     "NL" = "Nagaland",
                                     "MN" = "Manipur",
                                     "MZ" = "Mizoram",
                                     "TR" = "Tripura",
                                     "ML" = "Meghalaya",
                                     "WB" = "West Bengal",
                                     "MH" = "Maharashtra",
                                     "AP" = "Andhra Pradesh",
                                     "KA" = "Karnataka",
                                     "GA" = "Goa",
                                     "KL" = "Kerala",
                                     "Puducherry" = "Puducherry",
                                     "TN" = "Tamil Nadu",
                                     "AN" = "Andaman and Nicobar",
                                     "TS" = "Telangana",
                                     "UK" = "Uttarakhand",
                                     "RJ" = "Rajasthan",
                                     "UP" = "Uttar Pradesh",
                                     "BR" = "Bihar",
                                     "AS" = "Assam",
                                     "JH" = "Jharkhand",
                                     "OD" = "Odisha",
                                     "CT" = "Chhattisgarh", 
                                     "MP" = "Madhya Pradesh",
                                     "JK" = "Jammu and Kashmir",
                                     "GJ" = "Gujarat",
                                     "Lakshadweep" = "Lakshadweep",
                                "Dadra and Nagar Haveli" = "Dadra and Nagar Haveli")) %>%
                  filter(state != "Lakshadweep" & state != "Dadra and Nagar Haveli" & state != "Chandigarh" & state != "Daman and Diu" & state != "Puducherry" )

centers <- centers %>% 
  mutate(ex_state_ind = state)

theme_map <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace% 
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),  # deprecated
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.background=element_rect(fill="white", colour=NA),
legend.key=element_rect(colour="white"),
legend.key.size=unit(1.5, "lines"),
legend.position="right",
legend.text=element_text(size=15, family="Times"),
legend.title=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),  # deprecated
plot.background=element_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=element_text(size=rel(1.8), face="bold", hjust=0.5, family="Times"),
strip.background=element_rect(fill="white", colour="white"),
strip.text=element_text(size=rel(1.4), face="italic", family="Times")
)   
}


# Now calculate prevalence by state
temp.dat2 <- dhs_nomiss %>% 
  group_by(ex_state_ind) %>%
  mutate(multi_morbid = 100*weighted.mean(multi_morbid_dbl,sworld_weight_india, na.rm=TRUE)) %>% 
  filter(row_number()==1) %>% 
  dplyr::select(ex_state_ind, multi_morbid) %>% 
  filter(ex_state_ind!="Daman and Diu")  # Daman and Diu has a crazy high urban prev, so to not distort color scale kick out this invisible state


dat <- dat %>% 
  mutate(ex_state_ind = state) %>%
   mutate(ex_state_ind = fct_recode(ex_state_ind, 
                                     "Andaman and Nicobar Islands" = "Andaman and Nicobar",
                                     "Delhi" = "NCT of Delhi")) %>%
  filter(ex_state_ind!="Daman and Diu") 


dat$ex_state_ind <- factor(dat$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))

temp.dat2$ex_state_ind <- factor(temp.dat2$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))


map.dat2 <- left_join(dat, temp.dat2, by="ex_state_ind") # adds an id column to cvd_tempdat
map.dat2 <- inner_join(map, map.dat2, by = "id")


# Now plot the actual map - htn
htn_map2 <- ggplot() +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group),
         color = "#ffffff", fill = "#ececec", size = 0.25) +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group, fill=multi_morbid),
         color = "#ffffff", size = 0.25) +
  geom_text_repel(data = centers, 
                  aes(label = ex_state_ind, x = x, y = y, fontface=2), 
                  size = 7, segment.color = "black", segment.size = 0.3, family="Times") +
  coord_map() +
  scale_fill_distiller(palette = "OrRd", direction = 1, na.value = "grey80") +
  labs(x = "", y = "") +
  xlim(68, 98) + 
  ylim(7, 35) +
  ggtitle("Prevalence of ≥ 2 morbidities (%)") + 
  theme_map()
htn_map2

```

```{r prevalence among over 1 of over 2 morbidities among 15-49 year old}

dhs_nomiss4049 <- filter(dhs_nomiss_noNAinpsu, age<50)
###create PSU ID in dhs_nomiss4049 :
dhs_nomiss4049 <- dhs_nomiss4049 %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss4049$psuid <- as.factor(dhs_nomiss4049$psuid)




#ALL multi

dhs_nomiss4049 <- mutate(dhs_nomiss4049, 
                    zero_multi = ifelse(sum_multi_dbl==0,1,0),
                    one_multi = ifelse(sum_multi_dbl>=1,1,0),
                     two_multi = ifelse(sum_multi_dbl>=2,1,0),
                     three_multi = ifelse(sum_multi_dbl>=3,1,0),
                     four_multi = ifelse(sum_multi_dbl>=4,1,0),
                    five_multi = ifelse(sum_multi_dbl>=5,1,0))

over1multi <- filter(dhs_nomiss4049, one_multi==1)

 svy_over1multi <- over1multi %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(two_multi, three_multi, four_multi, five_multi))
  
  prevtotover1multi <- svy_over1multi %>%
    summarize( two_multi_pop = survey_mean(two_multi, proportion=TRUE, vartype = "ci")*100,
               three_multi_pop = survey_mean(three_multi, proportion=TRUE, vartype = "ci")*100,
               four_multi_pop = survey_mean(four_multi, proportion=TRUE, vartype = "ci")*100,
               five_multi_pop = survey_mean(five_multi, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotover1multi, " prevalence over 2,3,4,5 morbidities among individuals with over 1.csv")
  




```

```{r prevalence among over 1 of over 2 morbidities}

#create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)





#ALL multi

dhs_nomiss_noNAinpsu <- mutate(dhs_nomiss_noNAinpsu, 
                    zero_multi = ifelse(sum_multi_dbl==0,1,0),
                    one_multi = ifelse(sum_multi_dbl>=1,1,0),
                     two_multi = ifelse(sum_multi_dbl>=2,1,0),
                     three_multi = ifelse(sum_multi_dbl>=3,1,0),
                     four_multi = ifelse(sum_multi_dbl>=4,1,0),
                    five_multi = ifelse(sum_multi_dbl>=5,1,0))

over1multi <- filter(dhs_nomiss_noNAinpsu, one_multi==1)

 svy_over1multi <- over1multi %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(zero_multi, one_multi, two_multi, three_multi, four_multi,five_multi))
  
  prevtotover1multi <- svy_over1multi %>%
     summarize(zero_multi_pop = survey_mean(zero_multi, proportion=TRUE, vartype = "ci")*100,
              one_multi_pop = survey_mean(one_multi, proportion=TRUE, vartype = "ci")*100,
              two_multi_pop = survey_mean(two_multi, proportion=TRUE, vartype = "ci")*100,
              three_multi_pop = survey_mean(three_multi, proportion=TRUE, vartype = "ci")*100,
              four_multi_pop = survey_mean(four_multi, proportion=TRUE, vartype = "ci")*100,
              five_multi_pop = survey_mean(five_multi, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotover1multi, " prevalence over 2 morbidities among individuals with over 1.csv")
  



```

```{ }

###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)





###ALL multi

dhs_nomiss_noNAinpsu <- mutate(dhs_nomiss_noNAinpsu, 
                    zero_multi = ifelse(sum_multi_dbl==0,1,0),
                    one_multi = ifelse(sum_multi_dbl>=1,1,0),
                     two_multi = ifelse(sum_multi_dbl>=2,1,0),
                     three_multi = ifelse(sum_multi_dbl>=3,1,0),
                     four_multi = ifelse(sum_multi_dbl>=4,1,0))

over1multi <- filter(dhs_nomiss_noNAinpsu, one_multi==1)

 svy_over1multi <- over1multi %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(two_multi))
  
  prevtotover1multi <- svy_over1multi %>%
    summarize( two_multi_pop = survey_mean(two_multi, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotover1multi, " prevalence over 2 morbidities among individuals with over 1.csv")
  

```


```{r prevalence of over 2 among 40 to 49 year old}


dhs_nomiss4049 <- filter(dhs_nomiss_noNAinpsu, age>39 & age<50)
###create PSU ID in dhs_nomiss4049 :
dhs_nomiss4049 <- dhs_nomiss4049 %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss4049$psuid <- as.factor(dhs_nomiss4049$psuid)





###ALL multi

dhs_nomiss4049 <- mutate(dhs_nomiss4049, 
                    zero_multi = ifelse(sum_multi_dbl==0,1,0),
                    one_multi = ifelse(sum_multi_dbl>=1,1,0),
                     two_multi = ifelse(sum_multi_dbl>=2,1,0),
                     three_multi = ifelse(sum_multi_dbl>=3,1,0),
                     four_multi = ifelse(sum_multi_dbl>=4,1,0))


 svy_over1multi <- dhs_nomiss4049 %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(two_multi))
  
  prevtotover1multi <- svy_over1multi %>%
    summarize( two_multi_pop = survey_mean(two_multi, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotover1multi, " prevalence over 2 morbidities among individuals aged 40-49.csv")
  



```


```{r prevalence of over 2 among <40 years}


dhs_nomiss40 <- filter(dhs_nomiss_noNAinpsu, age<40)
###create PSU ID in dhs_nomiss40 :
dhs_nomiss40 <- dhs_nomiss40 %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss40$psuid <- as.factor(dhs_nomiss40$psuid)





###ALL multi

dhs_nomiss40 <- mutate(dhs_nomiss40, 
                    zero_multi = ifelse(sum_multi_dbl==0,1,0),
                    one_multi = ifelse(sum_multi_dbl>=1,1,0),
                     two_multi = ifelse(sum_multi_dbl>=2,1,0),
                     three_multi = ifelse(sum_multi_dbl>=3,1,0),
                     four_multi = ifelse(sum_multi_dbl>=4,1,0))


 svy_over1multi <- dhs_nomiss40 %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(two_multi))
  
  prevtotover1multi <- svy_over1multi %>%
    summarize( two_multi_pop = survey_mean(two_multi, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotover1multi, " prevalence over 2 morbidities among individuals aged 40-49.csv")
  



```





```{r prevalence of over 2 among poorest}


dhs_nomiss_poorest <- filter(dhs_nomiss_noNAinpsu, wealth_quintile_rurb_lab== "Q1 (Poorest)")
###create PSU ID in dhs_nomiss_poorest :
dhs_nomiss_poorest <- dhs_nomiss_poorest %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_poorest$psuid <- as.factor(dhs_nomiss_poorest$psuid)





###ALL multi

dhs_nomiss_poorest <- mutate(dhs_nomiss_poorest, 
                    zero_multi = ifelse(sum_multi_dbl==0,1,0),
                    one_multi = ifelse(sum_multi_dbl>=1,1,0),
                     two_multi = ifelse(sum_multi_dbl>=2,1,0),
                     three_multi = ifelse(sum_multi_dbl>=3,1,0),
                     four_multi = ifelse(sum_multi_dbl>=4,1,0))


 svy_over1multi <- dhs_nomiss_poorest %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(two_multi))
  
  prevtotover1multi <- svy_over1multi %>%
    summarize( two_multi_pop = survey_mean(two_multi, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotover1multi, " prevalence over 2 morbidities among poorest DHS.csv")
  



```



```{r prevalence of asthma}



###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)





###ALL multi


 svy_over1multi <- dhs_nomiss_noNAinpsu %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(asthma))
  
  prevtotover1multi <- svy_over1multi %>%
    summarize( two_multi_pop = survey_mean(asthma, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotover1multi, " prevalence asthma DHS.csv")
  



```





```{r OVERALL prevalence of individuals with at least one risc factor, CVD risc factor }

###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)





###ALL multi

dhs_nomiss_noNAinpsu <- mutate(dhs_nomiss_noNAinpsu, 
                    zero_multi = ifelse(sum_multi_dbl==0,1,0),
                    one_multi = ifelse(sum_multi_dbl>=1,1,0),
                     two_multi = ifelse(sum_multi_dbl>=2,1,0),
                     three_multi = ifelse(sum_multi_dbl>=3,1,0),
                     four_multi = ifelse(sum_multi_dbl>=4,1,0),
                   five_multi = ifelse(sum_multi_dbl>=5,1,0))

 svy_overallmulti <- dhs_nomiss_noNAinpsu %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( zero_multi, one_multi, two_multi, three_multi, four_multi, five_multi))
  
  prevtotoverallmulti <- svy_overallmulti %>%
    summarize(zero_multi_pop = survey_mean(zero_multi, proportion=TRUE, vartype = "ci")*100,
              one_multi_pop = survey_mean(one_multi, proportion=TRUE, vartype = "ci")*100,
              two_multi_pop = survey_mean(two_multi, proportion=TRUE, vartype = "ci")*100,
              three_multi_pop = survey_mean(three_multi, proportion=TRUE, vartype = "ci")*100,
              four_multi_pop = survey_mean(four_multi, proportion=TRUE, vartype = "ci")*100,
              five_multi_pop = survey_mean(five_multi, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotoverallmulti, "OVERALL prevalence multimorbidity.csv")
  



```



```{r OVERALL prevalence by sex}


###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)


###ALL multi

dhs_nomiss_noNAinpsu <- mutate(dhs_nomiss_noNAinpsu, 
                    zero_multi = ifelse(sum_multi_dbl==0,1,0),
                    one_multi = ifelse(sum_multi_dbl>=1,1,0),
                     two_multi = ifelse(sum_multi_dbl>=2,1,0),
                     three_multi = ifelse(sum_multi_dbl>=3,1,0),
                     four_multi = ifelse(sum_multi_dbl>=4,1,0))

 svy_overallmultisex <- dhs_nomiss_noNAinpsu %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(sex,  two_multi))
  
  prevtotoverallmultisex <- svy_overallmultisex %>%
    group_by(sex)%>%
    summarize(two_multi_pop = survey_mean(two_multi, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotoverallmultisex, "OVERALL prevalence multimorbidity by sex.csv")
  



```



```{r OVERALL prevalence by urban rural}


###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)



###ALL multi

dhs_nomiss_noNAinpsu <- mutate(dhs_nomiss_noNAinpsu, 
                    zero_multi = ifelse(sum_multi_dbl==0,1,0),
                    one_multi = ifelse(sum_multi_dbl>=1,1,0),
                     two_multi = ifelse(sum_multi_dbl>=2,1,0),
                     three_multi = ifelse(sum_multi_dbl>=3,1,0),
                     four_multi = ifelse(sum_multi_dbl>=4,1,0))

 svy_overallmultiurban <- dhs_nomiss_noNAinpsu %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(urban, zero_multi, one_multi, two_multi, three_multi, four_multi))
  
  prevtotoverallmultiurban <- svy_overallmultiurban %>%
    group_by(urban)%>%
    summarize(two_multi_pop = survey_mean(two_multi, proportion=TRUE, vartype = "ci")*100) 
  write_csv(prevtotoverallmultiurban, "OVERALL prevalence multimorbidity by urban.csv")
  
svychisq(~urban+two_multi,svy_overallmultiurban, statistic="Chisq")


```


```{r  older and younger 39 of the ones with >1}

###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)

####ALL RISC

morethan2 <- filter(dhs_nomiss_noNAinpsu, multi_morbid_dbl==1)


morethan2 <-morethan2 %>%
  mutate(age_grp_morethan2 =ifelse(age<40, 0,
                                         ifelse(age>39,1,NA)))

 svy_ex_htn_broad_ind <- morethan2 %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(age_grp_morethan2))
  
  prevtot <- svy_ex_htn_broad_ind %>%
    summarize(multi_risc_dbl_pop = survey_mean(age_grp_morethan2, proportion=TRUE, vartype = "ci")*100)
  write_csv(prevtot, "Prevalence multimorbidity by age group.csv")
```

```{r prevalence of multimorbidity among individuals with at least one risc factor, CVD risc factor }

###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)

####ALL RISC

atleast1 <- filter(dhs_nomiss_noNAinpsu,sum_multi_dbl>=1)


 svy_ex_htn_broad_ind <- atleast1 %>% 
     as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(multi_morbid_dbl))
  
  prevtot <- svy_ex_htn_broad_ind %>%
    summarize(multi_morbid_dbl_pop = survey_mean(multi_morbid_dbl, proportion=TRUE, vartype = "ci")*100)
  write_csv(prevtot, "Prevalence multimorbidity among individuals with at least one morbidity.csv")



```

```{r table 1 DHS}

# Summary dhs_nomiss

dhs_nomiss$fast <- as.factor(dhs_nomiss$fast)
dhs_nomiss$ex_diab_broad_ind <- as.factor(dhs_nomiss$ex_diab_broad_ind)
dhs_nomiss$educat_lcl <- as.factor(dhs_nomiss$educat_lcl)

dhs_nomiss$ex_htn_broad_ind <- as.factor(dhs_nomiss$ex_htn_broad_ind)
dhs_nomiss$asthma <- as.factor(dhs_nomiss$asthma)
dhs_nomiss$obese <- as.factor(dhs_nomiss$obese)
dhs_nomiss$tobacco_smoked <- as.factor(dhs_nomiss$tobacco_smoked)
dhs_nomiss$tobacco_smokeless <- as.factor(dhs_nomiss$tobacco_smokeless)


table1names <- c( "age_grp_old",
                  "educat_lcl", "wealth_quintile_rurb_lab", "marriednames", "urban_lab", "obese", "tobacco_smoked","tobacco_smokeless", "ex_diab_broad_ind", "ex_htn_broad_ind", "asthma", "anemia")




totalwithmiss <- CreateTableOne(vars = table1names, data=dhs_nomiss, includeNA=T)
total <- print(totalwithmiss)

write.csv(total, file="dhs_nomiss summary.csv")

sexwithmiss <- CreateTableOne(vars = table1names, data=dhs_nomiss, strata=c("female_lab"), includeNA=T)
sexdhs_nomiss <- print(sexwithmiss)

write.csv(sexdhs_nomiss, file= "dhs_nomiss summary per sex.csv")

```


```{r table 1 missing DHS}

Missing <- filter(dhs, is.na(ex_diab_broad_ind)==T | is.na(ex_htn_broad_ind)==T | is.na(anemia)==T | is.na(asthma)==T | is.na(obese)==T)

Missing <- dplyr::mutate(Missing, age_grp_old = ifelse(age<=24 , "15-24", 
                                                             ifelse(age>24 &  age<=34, "25-34",
                                                                    ifelse(age>34 &  age<=44, "35-44",
                                                                           ifelse(age>44 &  age<=54, "45-54", NA)))))


# Summary Missing

Missing$fast <- as.factor(Missing$fast)
Missing$ex_diab_broad_ind <- as.factor(Missing$ex_diab_broad_ind)
Missing$educat_lcl <- as.factor(Missing$educat_lcl)

Missing$ex_htn_broad_ind <- as.factor(Missing$ex_htn_broad_ind)
Missing$asthma <- as.factor(Missing$asthma)
Missing$obese <- as.factor(Missing$obese)
Missing$tobacco_smoked <- as.factor(Missing$tobacco_smoked)
Missing$tobacco_smokeless <- as.factor(Missing$tobacco_smokeless)


table1names <- c( "age_grp_old",
                  "educat_lcl", "wealth_quintile_rurb_lab", "marriednames", "urban_lab", "obese", "tobacco_smoked","tobacco_smokeless", "ex_diab_broad_ind", "ex_htn_broad_ind", "asthma", "anemia")




totalwithmiss <- CreateTableOne(vars = table1names, data=Missing, includeNA=T)
total <- print(totalwithmiss)

write.csv(total, file="Missing summary.csv")

sexwithmiss <- CreateTableOne(vars = table1names, data=Missing, strata=c("female_lab"), includeNA=T)
sexMissing <- print(sexwithmiss)

write.csv(sexMissing, file= "Missing summary per sex.csv")

```

```{r table 1  DHS only HIV dataset}


dhs_nomiss$obese <- as.factor(dhs_nomiss$obese)
dhs_nomiss$tobacco_smoked <- as.factor(dhs_nomiss$tobacco_smoked)
dhs_nomiss$ex_htn_broad_ind <- as.factor(dhs_nomiss$ex_htn_broad_ind)
dhs_nomiss$sev_underweight <- as.factor(dhs_nomiss$sev_underweight)
dhs_nomiss$asthma <- as.factor(dhs_nomiss$asthma)
dhs_nomiss$ex_htn_broad_ind <- as.factor(dhs_nomiss$ex_htn_broad_ind)
dhs_nomiss$tobacco_smokeless <- as.factor(dhs_nomiss$tobacco_smokeless)


dhs_nomiss_HIV <- filter(dhs_nomiss, hiv03==1)

# Summary dhs_nomiss

dhs_nomiss_HIV$fast <- as.factor(dhs_nomiss_HIV$fast)



table1names <- c( "age_grp_old",
                  "educatnames", "wealth_quintile_rurb_lab", "marriednames", "urban_lab", "obese", "tobacco_smoked", "tobacco_smokeless", "ex_diab_broad_ind", "ex_htn_broad_ind", "sev_underweight", "ex_diab_broad_ind", "ex_htn_broad_ind", "asthma", "anemia")




totalwithmiss <- CreateTableOne(vars = table1names, data=dhs_nomiss_HIV, includeNA=T)
total <- print(totalwithmiss)

write.csv(total, file="dhs_nomiss_HIV summary.csv")

sexwithmiss <- CreateTableOne(vars = table1names, data=dhs_nomiss_HIV, strata=c("female_lab"), includeNA=T)
sexdhs_nomiss_HIV <- print(sexwithmiss)

write.csv(sexdhs_nomiss_HIV, file= "dhs_nomiss_HIV summary per sex.csv")

```



```{r prevalence HIV in DHS}



dhs_nomiss <- mutate(dhs_nomiss, 
                     psu = ifelse(psu==1, NA, psu))

summary(dhs_nomiss$psu)
###create PSU ID in dhs_nomiss:
dhs_nomiss <- dhs_nomiss %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss$psuid <- as.factor(dhs_nomiss$psuid) 





dhs_nomiss_noNAinpsu <- filter(dhs_nomiss, is.na(psu)==F)



dhs_nomiss_noNAinpsu[which(dhs_nomiss_noNAinpsu$hiv03==7), "hiv03"]<-0


dhs_nomiss_HIVtest <- filter(dhs_nomiss_noNAinpsu, is.na(hiv01)==F)

###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_HIVtest %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)


svy_treated <- dhs_nomiss_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = sworld_weight_india,
                   variables = c(hiv03))
  
  prevtot <- svy_treated %>%
    summarize(HIV_pop = survey_mean(hiv03, proportion=TRUE, vartype = "ci"))
  write_csv(prevtot, "HIV prevalence.csv")



```


```{r morbidity prevalence by state }


###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)


svy_treated <- dhs_nomiss_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = sworld_weight_india,
                   variables = c(ex_state_ind, multi_morbid_dbl))
  
  prevtot <- svy_treated %>%
    group_by(ex_state_ind) %>%
    summarize(multi_morbid_dbl_pop = survey_mean(multi_morbid_dbl, proportion=TRUE, vartype = "ci"))
  write_csv(prevtot, "Multimorbidity prevalence by state check as percentage.csv")






```


```{r morbidity prevalence by state and rural/urban}


###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)


svy_treated <- dhs_nomiss_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = sworld_weight_india,
                   variables = c(ex_state_ind, multi_morbid_dbl, urban))
  
  prevtot <- svy_treated %>%
    group_by(ex_state_ind, urban) %>%
    summarize(multi_morbid_dbl_pop = survey_mean(multi_morbid_dbl, proportion=TRUE, vartype = "ci")*100)
  write_csv(prevtot, "Multimorbidity prevalence by state and rural urban check.csv")


svychisq(~urban+multi_morbid_dbl, prevtot, statistic="Chisq")



```





```{r morbidity prevalence by zone_new }


###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)


svy_treated <- dhs_nomiss_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = sworld_weight_india,
                   variables = c(zone_new, multi_morbid_dbl))
  
  prevtot <- svy_treated %>%
    group_by(zone_new) %>%
    summarize(multi_morbid_dbl_pop = survey_mean(multi_morbid_dbl, proportion=TRUE, vartype = "ci")*100)
  write_csv(prevtot, "Multimorbidity prevalence by zone_new.csv")






```



```{r prevalence 10 year age group 1,2,3,4 morbidities}

###create PSU ID in dhs_nomiss_noNAinpsu :
dhs_nomiss_noNAinpsu <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu$psuid <- as.factor(dhs_nomiss_noNAinpsu$psuid)







svy_treated <- dhs_nomiss_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = sworld_weight_india,
                   variables = c(age_grp_old, zero_morb, one_morb, two_morb, three_morb, four_morb, five_morb, six_morb,seven_morb))
  
  prevtot <- svy_treated %>%
    group_by(age_grp_old) %>%
    summarize(zero_morb_pop = survey_mean(zero_morb, proportion=TRUE, vartype = "ci"),
              one_morb_pop = survey_mean(one_morb, proportion=TRUE, vartype = "ci"),
              two_morb_pop = survey_mean(two_morb, proportion=TRUE, vartype = "ci"),
              three_morb_pop = survey_mean(three_morb, proportion=TRUE, vartype = "ci"),
              four_morb_pop = survey_mean(four_morb, proportion=TRUE, vartype = "ci"),
              five_morb_pop = survey_mean(five_morb, proportion=TRUE, vartype = "ci"),
              six_morb_pop = survey_mean(six_morb, proportion=TRUE, vartype = "ci"),
              seven_morb_pop = survey_mean(seven_morb, proportion=TRUE, vartype = "ci")) 
  write_csv(prevtot, "10 year age group prevalence multimorb 19 07.csv")


 ######FORMATTING
`10.year.age.group.prevalence.multimorb.19.07` <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/10 year age group prevalence multimorb 19 07.csv")
aware <- `10.year.age.group.prevalence.multimorb.19.07`

aware <- mutate(aware,
                zero_morb_pop = zero_morb_pop*100,
                one_morb_pop = one_morb_pop*100,
                two_morb_pop = two_morb_pop*100,
                three_morb_pop = three_morb_pop*100,
                four_morb_pop = four_morb_pop*100,
                five_morb_pop = five_morb_pop*100,
                six_morb_pop = six_morb_pop*100,
                zero_morb_pop_low = zero_morb_pop_low*100,
                one_morb_pop_low = one_morb_pop_low*100,
                two_morb_pop_low = two_morb_pop_low*100,
                three_morb_pop_low = three_morb_pop_low*100,
                four_morb_pop_low = four_morb_pop_low*100,
                five_morb_pop_low = five_morb_pop_low*100,
                six_morb_pop_low = six_morb_pop_low*100,
                zero_morb_pop_upp = zero_morb_pop_upp*100,
                one_morb_pop_upp = one_morb_pop_upp*100,
                two_morb_pop_upp = two_morb_pop_upp*100,
                three_morb_pop_upp = three_morb_pop_upp*100,
                four_morb_pop_upp = four_morb_pop_upp*100,
                five_morb_pop_upp = five_morb_pop_upp*100,
                six_morb_pop_upp = six_morb_pop_upp*100)



aware <- mutate(aware,
                zero_morb_pop = round(zero_morb_pop,2),
                one_morb_pop = round(one_morb_pop,2),
                two_morb_pop = round(two_morb_pop,2),
                three_morb_pop = round(three_morb_pop,2),
                four_morb_pop = round(four_morb_pop,2),
                five_morb_pop = round(five_morb_pop,2),
                six_morb_pop = round(six_morb_pop,2),
                zero_morb_pop_low = round(zero_morb_pop_low,2),
                one_morb_pop_low = round(one_morb_pop_low,2),
                two_morb_pop_low = round(two_morb_pop_low,2),
                three_morb_pop_low = round(three_morb_pop_low,2),
                four_morb_pop_low = round(four_morb_pop_low,2),
                five_morb_pop_low = round(five_morb_pop_low,2),
                six_morb_pop_low = round(six_morb_pop_low,2),
                zero_morb_pop_upp = round(zero_morb_pop_upp,2),
                one_morb_pop_upp = round(one_morb_pop_upp,2),
                two_morb_pop_upp = round(two_morb_pop_upp,2),
                three_morb_pop_upp = round(three_morb_pop_upp,2),
                four_morb_pop_upp = round(four_morb_pop_upp,2),
                five_morb_pop_upp = round(five_morb_pop_upp,2),
                six_morb_pop_upp = round(six_morb_pop_upp,2))


aware$zero_morb_pop <- sprintf("%.2f", aware$zero_morb_pop)
aware$one_morb_pop <- sprintf("%.2f", aware$one_morb_pop)
aware$two_morb_pop <- sprintf("%.2f", aware$two_morb_pop)
aware$three_morb_pop <- sprintf("%.2f", aware$three_morb_pop)
aware$four_morb_pop <- sprintf("%.2f", aware$four_morb_pop)
aware$five_morb_pop <- sprintf("%.2f", aware$five_morb_pop)
aware$six_morb_pop <- sprintf("%.2f", aware$six_morb_pop)
aware$zero_morb_pop_low <- sprintf("%.2f", aware$zero_morb_pop_low)
aware$one_morb_pop_low <- sprintf("%.2f", aware$one_morb_pop_low)
aware$two_morb_pop_low <- sprintf("%.2f", aware$two_morb_pop_low)
aware$three_morb_pop_low <- sprintf("%.2f", aware$three_morb_pop_low)
aware$four_morb_pop_low <- sprintf("%.2f", aware$four_morb_pop_low)
aware$five_morb_pop_low <- sprintf("%.2f", aware$five_morb_pop_low)
aware$six_morb_pop_low <- sprintf("%.2f", aware$six_morb_pop_low)
aware$zero_morb_pop_upp <- sprintf("%.2f", aware$zero_morb_pop_upp)
aware$one_morb_pop_upp <- sprintf("%.2f", aware$one_morb_pop_upp)
aware$two_morb_pop_upp <- sprintf("%.2f", aware$two_morb_pop_upp)
aware$three_morb_pop_upp <- sprintf("%.2f", aware$three_morb_pop_upp)
aware$four_morb_pop_upp <- sprintf("%.2f", aware$four_morb_pop_upp)
aware$five_morb_pop_upp <- sprintf("%.2f", aware$five_morb_pop_upp)
aware$six_morb_pop_upp <- sprintf("%.2f", aware$six_morb_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_morb_pop_low, zero_morb_pop_upp, sep="-"),
                citempone = str_c(one_morb_pop_low, one_morb_pop_upp, sep="-"),
                citemptwo = str_c(two_morb_pop_low, two_morb_pop_upp, sep="-"),
                citempthree = str_c(three_morb_pop_low, three_morb_pop_upp, sep="-"),
                citempfour = str_c(four_morb_pop_low, four_morb_pop_upp, sep="-"),
                citempfive = str_c(five_morb_pop_low, five_morb_pop_upp, sep="-"),
                citempsix = str_c(six_morb_pop_low, six_morb_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_morb_pop, cizero, sep=" "),
                rrone = str_c(one_morb_pop, cione, sep=" "),
                rrtwo = str_c(two_morb_pop, citwo, sep=" "),
                rrthree = str_c(three_morb_pop, cithree, sep=" "),
                rrfour = str_c(four_morb_pop, cifour, sep=" "),
                rrfive = str_c(five_morb_pop, cifive, sep=" "),
                rrsix = str_c(six_morb_pop, cisix, sep=" "))




write.csv(aware, "10 year prev morb 19 07.csv")



```



```{r prevalence 10 year age group morbidities MEN vs Women}

dhs_nomiss_noNAinpsu_men <- filter(dhs_nomiss_noNAinpsu, sex==0)


###create PSU ID in dhs_nomiss_noNAinpsu_men :
dhs_nomiss_noNAinpsu_men <- dhs_nomiss_noNAinpsu_men %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu_men$psuid <- as.factor(dhs_nomiss_noNAinpsu_men$psuid)







svy_treated <- dhs_nomiss_noNAinpsu_men %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = sworld_weight_india,
                   variables = c(age_grp_old, zero_morb, one_morb, two_morb, three_morb, four_morb, five_morb, six_morb,seven_morb))
  
  prevtot <- svy_treated %>%
    group_by(age_grp_old) %>%
    summarize(zero_morb_pop = survey_mean(zero_morb, proportion=TRUE, vartype = "ci"),
              one_morb_pop = survey_mean(one_morb, proportion=TRUE, vartype = "ci"),
              two_morb_pop = survey_mean(two_morb, proportion=TRUE, vartype = "ci"),
              three_morb_pop = survey_mean(three_morb, proportion=TRUE, vartype = "ci"),
              four_morb_pop = survey_mean(four_morb, proportion=TRUE, vartype = "ci"),
              five_morb_pop = survey_mean(five_morb, proportion=TRUE, vartype = "ci"),
              six_morb_pop = survey_mean(six_morb, proportion=TRUE, vartype = "ci"),
              seven_morb_pop = survey_mean(seven_morb, proportion=TRUE, vartype = "ci")) 
  write_csv(prevtot, "10 year age group prevalence multimorb 19 07 men.csv")


 ######FORMATTING
`10.year.age.group.prevalence.multimorb.19.07` <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/10 year age group prevalence multimorb 19 07 men.csv")
aware <- `10.year.age.group.prevalence.multimorb.19.07`

aware <- mutate(aware,
                zero_morb_pop = zero_morb_pop*100,
                one_morb_pop = one_morb_pop*100,
                two_morb_pop = two_morb_pop*100,
                three_morb_pop = three_morb_pop*100,
                four_morb_pop = four_morb_pop*100,
                five_morb_pop = five_morb_pop*100,
                six_morb_pop = six_morb_pop*100,
                zero_morb_pop_low = zero_morb_pop_low*100,
                one_morb_pop_low = one_morb_pop_low*100,
                two_morb_pop_low = two_morb_pop_low*100,
                three_morb_pop_low = three_morb_pop_low*100,
                four_morb_pop_low = four_morb_pop_low*100,
                five_morb_pop_low = five_morb_pop_low*100,
                six_morb_pop_low = six_morb_pop_low*100,
                zero_morb_pop_upp = zero_morb_pop_upp*100,
                one_morb_pop_upp = one_morb_pop_upp*100,
                two_morb_pop_upp = two_morb_pop_upp*100,
                three_morb_pop_upp = three_morb_pop_upp*100,
                four_morb_pop_upp = four_morb_pop_upp*100,
                five_morb_pop_upp = five_morb_pop_upp*100,
                six_morb_pop_upp = six_morb_pop_upp*100)



aware <- mutate(aware,
                zero_morb_pop = round(zero_morb_pop,2),
                one_morb_pop = round(one_morb_pop,2),
                two_morb_pop = round(two_morb_pop,2),
                three_morb_pop = round(three_morb_pop,2),
                four_morb_pop = round(four_morb_pop,2),
                five_morb_pop = round(five_morb_pop,2),
                six_morb_pop = round(six_morb_pop,2),
                zero_morb_pop_low = round(zero_morb_pop_low,2),
                one_morb_pop_low = round(one_morb_pop_low,2),
                two_morb_pop_low = round(two_morb_pop_low,2),
                three_morb_pop_low = round(three_morb_pop_low,2),
                four_morb_pop_low = round(four_morb_pop_low,2),
                five_morb_pop_low = round(five_morb_pop_low,2),
                six_morb_pop_low = round(six_morb_pop_low,2),
                zero_morb_pop_upp = round(zero_morb_pop_upp,2),
                one_morb_pop_upp = round(one_morb_pop_upp,2),
                two_morb_pop_upp = round(two_morb_pop_upp,2),
                three_morb_pop_upp = round(three_morb_pop_upp,2),
                four_morb_pop_upp = round(four_morb_pop_upp,2),
                five_morb_pop_upp = round(five_morb_pop_upp,2),
                six_morb_pop_upp = round(six_morb_pop_upp,2))


aware$zero_morb_pop <- sprintf("%.2f", aware$zero_morb_pop)
aware$one_morb_pop <- sprintf("%.2f", aware$one_morb_pop)
aware$two_morb_pop <- sprintf("%.2f", aware$two_morb_pop)
aware$three_morb_pop <- sprintf("%.2f", aware$three_morb_pop)
aware$four_morb_pop <- sprintf("%.2f", aware$four_morb_pop)
aware$five_morb_pop <- sprintf("%.2f", aware$five_morb_pop)
aware$six_morb_pop <- sprintf("%.2f", aware$six_morb_pop)
aware$zero_morb_pop_low <- sprintf("%.2f", aware$zero_morb_pop_low)
aware$one_morb_pop_low <- sprintf("%.2f", aware$one_morb_pop_low)
aware$two_morb_pop_low <- sprintf("%.2f", aware$two_morb_pop_low)
aware$three_morb_pop_low <- sprintf("%.2f", aware$three_morb_pop_low)
aware$four_morb_pop_low <- sprintf("%.2f", aware$four_morb_pop_low)
aware$five_morb_pop_low <- sprintf("%.2f", aware$five_morb_pop_low)
aware$six_morb_pop_low <- sprintf("%.2f", aware$six_morb_pop_low)
aware$zero_morb_pop_upp <- sprintf("%.2f", aware$zero_morb_pop_upp)
aware$one_morb_pop_upp <- sprintf("%.2f", aware$one_morb_pop_upp)
aware$two_morb_pop_upp <- sprintf("%.2f", aware$two_morb_pop_upp)
aware$three_morb_pop_upp <- sprintf("%.2f", aware$three_morb_pop_upp)
aware$four_morb_pop_upp <- sprintf("%.2f", aware$four_morb_pop_upp)
aware$five_morb_pop_upp <- sprintf("%.2f", aware$five_morb_pop_upp)
aware$six_morb_pop_upp <- sprintf("%.2f", aware$six_morb_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_morb_pop_low, zero_morb_pop_upp, sep="-"),
                citempone = str_c(one_morb_pop_low, one_morb_pop_upp, sep="-"),
                citemptwo = str_c(two_morb_pop_low, two_morb_pop_upp, sep="-"),
                citempthree = str_c(three_morb_pop_low, three_morb_pop_upp, sep="-"),
                citempfour = str_c(four_morb_pop_low, four_morb_pop_upp, sep="-"),
                citempfive = str_c(five_morb_pop_low, five_morb_pop_upp, sep="-"),
                citempsix = str_c(six_morb_pop_low, six_morb_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_morb_pop, cizero, sep=" "),
                rrone = str_c(one_morb_pop, cione, sep=" "),
                rrtwo = str_c(two_morb_pop, citwo, sep=" "),
                rrthree = str_c(three_morb_pop, cithree, sep=" "),
                rrfour = str_c(four_morb_pop, cifour, sep=" "),
                rrfive = str_c(five_morb_pop, cifive, sep=" "),
                rrsix = str_c(six_morb_pop, cisix, sep=" "))




write.csv(aware, "10 year prev morb 19 07 men.csv")





dhs_nomiss_noNAinpsu_women <- filter(dhs_nomiss_noNAinpsu, sex==1)


###create PSU ID in dhs_nomiss_noNAinpsu_women :
dhs_nomiss_noNAinpsu_women <- dhs_nomiss_noNAinpsu_women %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noNAinpsu_women$psuid <- as.factor(dhs_nomiss_noNAinpsu_women$psuid)







svy_treated <- dhs_nomiss_noNAinpsu_women %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = sworld_weight_india,
                   variables = c(age_grp_old, zero_morb, one_morb, two_morb, three_morb, four_morb, five_morb, six_morb,seven_morb))
  
  prevtot <- svy_treated %>%
    group_by(age_grp_old) %>%
    summarize(zero_morb_pop = survey_mean(zero_morb, proportion=TRUE, vartype = "ci"),
              one_morb_pop = survey_mean(one_morb, proportion=TRUE, vartype = "ci"),
              two_morb_pop = survey_mean(two_morb, proportion=TRUE, vartype = "ci"),
              three_morb_pop = survey_mean(three_morb, proportion=TRUE, vartype = "ci"),
              four_morb_pop = survey_mean(four_morb, proportion=TRUE, vartype = "ci"),
              five_morb_pop = survey_mean(five_morb, proportion=TRUE, vartype = "ci"),
              six_morb_pop = survey_mean(six_morb, proportion=TRUE, vartype = "ci"),
              seven_morb_pop = survey_mean(seven_morb, proportion=TRUE, vartype = "ci")) 
  write_csv(prevtot, "10 year age group prevalence multimorb 19 07 women.csv")


 ######FORMATTING
`10.year.age.group.prevalence.multimorb.19.07` <- read.csv("~/Docuwoments/Public Health Files/Public Health/public health/cascades with morbidities/10 year age group prevalence multimorb 19 07 women.csv")
aware <- `10.year.age.group.prevalence.multimorb.19.07`

aware <- mutate(aware,
                zero_morb_pop = zero_morb_pop*100,
                one_morb_pop = one_morb_pop*100,
                two_morb_pop = two_morb_pop*100,
                three_morb_pop = three_morb_pop*100,
                four_morb_pop = four_morb_pop*100,
                five_morb_pop = five_morb_pop*100,
                six_morb_pop = six_morb_pop*100,
                zero_morb_pop_low = zero_morb_pop_low*100,
                one_morb_pop_low = one_morb_pop_low*100,
                two_morb_pop_low = two_morb_pop_low*100,
                three_morb_pop_low = three_morb_pop_low*100,
                four_morb_pop_low = four_morb_pop_low*100,
                five_morb_pop_low = five_morb_pop_low*100,
                six_morb_pop_low = six_morb_pop_low*100,
                zero_morb_pop_upp = zero_morb_pop_upp*100,
                one_morb_pop_upp = one_morb_pop_upp*100,
                two_morb_pop_upp = two_morb_pop_upp*100,
                three_morb_pop_upp = three_morb_pop_upp*100,
                four_morb_pop_upp = four_morb_pop_upp*100,
                five_morb_pop_upp = five_morb_pop_upp*100,
                six_morb_pop_upp = six_morb_pop_upp*100)



aware <- mutate(aware,
                zero_morb_pop = round(zero_morb_pop,2),
                one_morb_pop = round(one_morb_pop,2),
                two_morb_pop = round(two_morb_pop,2),
                three_morb_pop = round(three_morb_pop,2),
                four_morb_pop = round(four_morb_pop,2),
                five_morb_pop = round(five_morb_pop,2),
                six_morb_pop = round(six_morb_pop,2),
                zero_morb_pop_low = round(zero_morb_pop_low,2),
                one_morb_pop_low = round(one_morb_pop_low,2),
                two_morb_pop_low = round(two_morb_pop_low,2),
                three_morb_pop_low = round(three_morb_pop_low,2),
                four_morb_pop_low = round(four_morb_pop_low,2),
                five_morb_pop_low = round(five_morb_pop_low,2),
                six_morb_pop_low = round(six_morb_pop_low,2),
                zero_morb_pop_upp = round(zero_morb_pop_upp,2),
                one_morb_pop_upp = round(one_morb_pop_upp,2),
                two_morb_pop_upp = round(two_morb_pop_upp,2),
                three_morb_pop_upp = round(three_morb_pop_upp,2),
                four_morb_pop_upp = round(four_morb_pop_upp,2),
                five_morb_pop_upp = round(five_morb_pop_upp,2),
                six_morb_pop_upp = round(six_morb_pop_upp,2))


aware$zero_morb_pop <- sprintf("%.2f", aware$zero_morb_pop)
aware$one_morb_pop <- sprintf("%.2f", aware$one_morb_pop)
aware$two_morb_pop <- sprintf("%.2f", aware$two_morb_pop)
aware$three_morb_pop <- sprintf("%.2f", aware$three_morb_pop)
aware$four_morb_pop <- sprintf("%.2f", aware$four_morb_pop)
aware$five_morb_pop <- sprintf("%.2f", aware$five_morb_pop)
aware$six_morb_pop <- sprintf("%.2f", aware$six_morb_pop)
aware$zero_morb_pop_low <- sprintf("%.2f", aware$zero_morb_pop_low)
aware$one_morb_pop_low <- sprintf("%.2f", aware$one_morb_pop_low)
aware$two_morb_pop_low <- sprintf("%.2f", aware$two_morb_pop_low)
aware$three_morb_pop_low <- sprintf("%.2f", aware$three_morb_pop_low)
aware$four_morb_pop_low <- sprintf("%.2f", aware$four_morb_pop_low)
aware$five_morb_pop_low <- sprintf("%.2f", aware$five_morb_pop_low)
aware$six_morb_pop_low <- sprintf("%.2f", aware$six_morb_pop_low)
aware$zero_morb_pop_upp <- sprintf("%.2f", aware$zero_morb_pop_upp)
aware$one_morb_pop_upp <- sprintf("%.2f", aware$one_morb_pop_upp)
aware$two_morb_pop_upp <- sprintf("%.2f", aware$two_morb_pop_upp)
aware$three_morb_pop_upp <- sprintf("%.2f", aware$three_morb_pop_upp)
aware$four_morb_pop_upp <- sprintf("%.2f", aware$four_morb_pop_upp)
aware$five_morb_pop_upp <- sprintf("%.2f", aware$five_morb_pop_upp)
aware$six_morb_pop_upp <- sprintf("%.2f", aware$six_morb_pop_upp)





aware <- mutate(aware,
                citempzero = str_c(zero_morb_pop_low, zero_morb_pop_upp, sep="-"),
                citempone = str_c(one_morb_pop_low, one_morb_pop_upp, sep="-"),
                citemptwo = str_c(two_morb_pop_low, two_morb_pop_upp, sep="-"),
                citempthree = str_c(three_morb_pop_low, three_morb_pop_upp, sep="-"),
                citempfour = str_c(four_morb_pop_low, four_morb_pop_upp, sep="-"),
                citempfive = str_c(five_morb_pop_low, five_morb_pop_upp, sep="-"),
                citempsix = str_c(six_morb_pop_low, six_morb_pop_upp, sep="-"),
                bracketstart = "(", 
                bracketend = ")",
                cizero = str_c(bracketstart, citempzero, bracketend, sep=""),
                cione = str_c(bracketstart, citempone, bracketend, sep=""),
                citwo = str_c(bracketstart, citemptwo, bracketend, sep=""),
                cithree = str_c(bracketstart, citempthree, bracketend, sep=""),
                cifour = str_c(bracketstart, citempfour, bracketend, sep=""),
                cifive = str_c(bracketstart, citempfive, bracketend, sep=""),
                cisix = str_c(bracketstart, citempsix, bracketend, sep=""),
                rrzero = str_c(zero_morb_pop, cizero, sep=" "),
                rrone = str_c(one_morb_pop, cione, sep=" "),
                rrtwo = str_c(two_morb_pop, citwo, sep=" "),
                rrthree = str_c(three_morb_pop, cithree, sep=" "),
                rrfour = str_c(four_morb_pop, cifour, sep=" "),
                rrfive = str_c(five_morb_pop, cifive, sep=" "),
                rrsix = str_c(six_morb_pop, cisix, sep=" "))




write.csv(aware, "10 year prev morb 19 07 women.csv")




```



```{r heatmap multimorbidity wealth dhs}

multi_heatmap <- dhs_nomiss %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( wealth_quintile_rurb, age_grp, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(multi_morbid_dbl, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( wealth_quintile_rurb_lab, age_grp, urban_lab, multi_morbid_indiv, wealth_quintile_rurb, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=wealth_quintile_rurb_lab, y=age_grp)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat




```



```{r heatmap multimorbidity wealth dhs}

multi_heatmap <- dhs_nomiss %>%
  filter(is.na(educatnames)==FALSE & is.na(age)==FALSE) %>% 
  group_by( educatnames, age_grp, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(multi_morbid_dbl, sworld_weight_india, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( educatnames, age_grp, urban_lab, multi_morbid_indiv, educatnames, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=educatnames, y=age_grp)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Education",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(4/4)
multi_heat




```





```{r age-multi figure DHS}


#####dummy variables for each number of morbidites



library(spatstat)
statemean.dat <- dhs_nomiss %>%
  group_by(age) %>%
  mutate(zeromorb = weighted.mean(zero_morb, sworld_weight_india, na.rm=TRUE)*100,
         onemorb = weighted.mean(one_morb, sworld_weight_india, na.rm=TRUE)*100, 
         twomorb = weighted.mean(two_morb, sworld_weight_india, na.rm=TRUE)*100,
         threemorb = weighted.mean(three_morb, sworld_weight_india, na.rm=TRUE)*100,
         fourmorb = weighted.mean(four_morb, sworld_weight_india, na.rm=TRUE)*100,
         fivemorb = weighted.mean(five_morb, sworld_weight_india, na.rm=TRUE)*100,
         sixmorb =weighted.mean(six_morb, sworld_weight_india, na.rm=TRUE)*100,
         sevenmorb = weighted.mean(seven_morb, sworld_weight_india, na.rm=TRUE)*100,
         eightmorb = weighted.mean(eight_morb, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age, zeromorb, onemorb, twomorb, threemorb, fourmorb, fivemorb, sixmorb, sevenmorb, eightmorb)



#####Aware


stateawarefig <- statemean.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemean.dat$onemorb, x=statemean.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemean.dat$twomorb, x=statemean.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemean.dat$threemorb, x=statemean.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemean.dat$fourmorb, x=statemean.dat$age),span=0.5)+
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$onemorb ~ statemean.dat$age,span=0.6)),x=statemean.dat$age), alpha = 1, fill = "firebrick1") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$twomorb ~ statemean.dat$age,span=0.6)),x=statemean.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$threemorb ~ statemean.dat$age,span=0.6)),x=statemean.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$fourmorb ~ statemean.dat$age,span=0.6)),x=statemean.dat$age), alpha = 1, fill = "gray23") +
  theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        #legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c(20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20,30,40,50), limits=c(15, 54)) +
  coord_fixed(39/100, expand=F)
stateawarefig



stateawarefig <- statemean.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemean.dat$onemorb, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$twomorb, x=statemean.dat$age),span=0.6, se=F)+
  #geom_smooth(aes(y=statemean.dat$threemorb, x=statemean.dat$age),span=0.6, se=F)+
  # geom_smooth(aes(y=statemean.dat$fourmorb, x=statemean.dat$age),span=0.6, se=F)+
geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$onemorb ~ statemean.dat$age,span=0.6)),x=statemean.dat$age), alpha = 1, fill = "firebrick1", show.legend=TRUE) +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$twomorb ~ statemean.dat$age,span=0.6)),x=statemean.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$threemorb ~ statemean.dat$age,span=0.6)),x=statemean.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemean.dat$fourmorb ~ statemean.dat$age,span=0.6)),x=statemean.dat$age), alpha = 1, fill = "gray23") +
scale_colour_manual("", 
                      values = c(">=1 morb factor"="firebrick1", ">=2 morb factor"="firebrick3", 
                                 ">=3 morb factor"="firebrick4", ">=4 morb factor"="gray23" )) +
theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c( 20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20, 30, 40,49), limits=c(15, 49)) +
  coord_fixed(34/100, expand=F)
stateawarefig



```




```{r age-multi figure DHS Women vs. Men}

dhs_nomiss_men <- filter(dhs_nomiss, sex==0)


#####dummy variables for each number of morbidites



library(spatstat)
statemeanmultimorbidmen.dat <- dhs_nomiss_men %>%
  group_by(age) %>%
  mutate(zeromorb = weighted.mean(zero_morb, sworld_weight_india, na.rm=TRUE)*100,
         onemorb = weighted.mean(one_morb, sworld_weight_india, na.rm=TRUE)*100, 
         twomorb = weighted.mean(two_morb, sworld_weight_india, na.rm=TRUE)*100,
         threemorb = weighted.mean(three_morb, sworld_weight_india, na.rm=TRUE)*100,
         fourmorb = weighted.mean(four_morb, sworld_weight_india, na.rm=TRUE)*100,
         fivemorb = weighted.mean(five_morb, sworld_weight_india, na.rm=TRUE)*100,
         sixmorb =weighted.mean(six_morb, sworld_weight_india, na.rm=TRUE)*100,
         sevenmorb = weighted.mean(seven_morb, sworld_weight_india, na.rm=TRUE)*100,
         eightmorb = weighted.mean(eight_morb, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age, zeromorb, onemorb, twomorb, threemorb, fourmorb, fivemorb, sixmorb, sevenmorb, eightmorb)



#####Aware


stateawarefig <- statemeanmultimorbidmen.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemeanmultimorbidmen.dat$onemorb, x=statemeanmultimorbidmen.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidmen.dat$twomorb, x=statemeanmultimorbidmen.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidmen.dat$threemorb, x=statemeanmultimorbidmen.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidmen.dat$fourmorb, x=statemeanmultimorbidmen.dat$age),span=0.5)+
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidmen.dat$onemorb ~ statemeanmultimorbidmen.dat$age,span=1)),x=statemeanmultimorbidmen.dat$age), alpha = 1, fill = "firebrick1") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidmen.dat$twomorb ~ statemeanmultimorbidmen.dat$age,span=1)),x=statemeanmultimorbidmen.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidmen.dat$threemorb ~ statemeanmultimorbidmen.dat$age,span=1)),x=statemeanmultimorbidmen.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidmen.dat$fourmorb ~ statemeanmultimorbidmen.dat$age,span=1)),x=statemeanmultimorbidmen.dat$age), alpha = 1, fill = "gray23") +
  theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        #legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c(20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20,30,40,50), limits=c(15, 54)) +
  coord_fixed(39/100, expand=F)
stateawarefig




dhs_nomiss_women <- filter(dhs_nomiss, sex==1)


#####dummy variables for each number of morbidites



library(spatstat)
statemeanmultimorbidwomen.dat <- dhs_nomiss_women %>%
  group_by(age) %>%
  mutate(zeromorb = weighted.mean(zero_morb, sworld_weight_india, na.rm=TRUE)*100,
         onemorb = weighted.mean(one_morb, sworld_weight_india, na.rm=TRUE)*100, 
         twomorb = weighted.mean(two_morb, sworld_weight_india, na.rm=TRUE)*100,
         threemorb = weighted.mean(three_morb, sworld_weight_india, na.rm=TRUE)*100,
         fourmorb = weighted.mean(four_morb, sworld_weight_india, na.rm=TRUE)*100,
         fivemorb = weighted.mean(five_morb, sworld_weight_india, na.rm=TRUE)*100,
         sixmorb =weighted.mean(six_morb, sworld_weight_india, na.rm=TRUE)*100,
         sevenmorb = weighted.mean(seven_morb, sworld_weight_india, na.rm=TRUE)*100,
         eightmorb = weighted.mean(eight_morb, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age, zeromorb, onemorb, twomorb, threemorb, fourmorb, fivemorb, sixmorb, sevenmorb, eightmorb)



#####Aware


stateawarefig <- statemeanmultimorbidwomen.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemean.dat$onemorb, x=statemean.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidwomen.dat$twomorb, x=statemeanmultimorbidwomen.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidwomen.dat$threemorb, x=statemeanmultimorbidwomen.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidwomen.dat$fourmorb, x=statemeanmultimorbidwomen.dat$age),span=0.5)+
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidwomen.dat$onemorb ~ statemeanmultimorbidwomen.dat$age,span=1)),x=statemeanmultimorbidwomen.dat$age), alpha = 1, fill = "firebrick1") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidwomen.dat$twomorb ~ statemeanmultimorbidwomen.dat$age,span=1)),x=statemeanmultimorbidwomen.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidwomen.dat$threemorb ~ statemeanmultimorbidwomen.dat$age,span=1)),x=statemeanmultimorbidwomen.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidwomen.dat$fourmorb ~ statemeanmultimorbidwomen.dat$age,span=1)),x=statemeanmultimorbidwomen.dat$age), alpha = 1, fill = "gray23") +
  theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        #legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c(20, 40,60,80,100), limits=c(0, 100)) +
  scale_x_continuous(breaks = c(20,30,40,49), limits=c(15, 49)) +
  coord_fixed(34/100, expand=F)
stateawarefig








```




```{r age-multi figure DHS Rural vs urban}

dhs_nomiss_urban <- filter(dhs_nomiss, urban==1)


#####dummy variables for each number of morbidites



library(spatstat)
statemeanmultimorbidurban.dat <- dhs_nomiss_urban %>%
  group_by(age) %>%
  mutate(zeromorb = weighted.mean(zero_morb, sworld_weight_india, na.rm=TRUE)*100,
         onemorb = weighted.mean(one_morb, sworld_weight_india, na.rm=TRUE)*100, 
         twomorb = weighted.mean(two_morb, sworld_weight_india, na.rm=TRUE)*100,
         threemorb = weighted.mean(three_morb, sworld_weight_india, na.rm=TRUE)*100,
         fourmorb = weighted.mean(four_morb, sworld_weight_india, na.rm=TRUE)*100,
         fivemorb = weighted.mean(five_morb, sworld_weight_india, na.rm=TRUE)*100,
         sixmorb =weighted.mean(six_morb, sworld_weight_india, na.rm=TRUE)*100,
         sevenmorb = weighted.mean(seven_morb, sworld_weight_india, na.rm=TRUE)*100,
         eightmorb = weighted.mean(eight_morb, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age, zeromorb, onemorb, twomorb, threemorb, fourmorb, fivemorb, sixmorb, sevenmorb, eightmorb)



#####Aware


stateawarefig <- statemeanmultimorbidurban.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemeanmultimorbidurban.dat$onemorb, x=statemeanmultimorbidurban.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidurban.dat$twomorb, x=statemeanmultimorbidurban.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidurban.dat$threemorb, x=statemeanmultimorbidurban.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidurban.dat$fourmorb, x=statemeanmultimorbidurban.dat$age),span=0.5)+
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidurban.dat$onemorb ~ statemeanmultimorbidurban.dat$age,span=1)),x=statemeanmultimorbidurban.dat$age), alpha = 1, fill = "firebrick1") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidurban.dat$twomorb ~ statemeanmultimorbidurban.dat$age,span=1)),x=statemeanmultimorbidurban.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidurban.dat$threemorb ~ statemeanmultimorbidurban.dat$age,span=1)),x=statemeanmultimorbidurban.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidurban.dat$fourmorb ~ statemeanmultimorbidurban.dat$age,span=1)),x=statemeanmultimorbidurban.dat$age), alpha = 1, fill = "gray23") +
  theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        #legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c(10, 20,30,40,50,60,70), limits=c(0, 70)) +
  scale_x_continuous(breaks = c(20,30,40,49), limits=c(15, 49)) +
  coord_fixed(34/70, expand=F)
stateawarefig




dhs_nomiss_rural <- filter(dhs_nomiss, urban==0)


#####dummy variables for each number of morbidites



library(spatstat)
statemeanmultimorbidrural.dat <- dhs_nomiss_rural %>%
  group_by(age) %>%
  mutate(zeromorb = weighted.mean(zero_morb, sworld_weight_india, na.rm=TRUE)*100,
         onemorb = weighted.mean(one_morb, sworld_weight_india, na.rm=TRUE)*100, 
         twomorb = weighted.mean(two_morb, sworld_weight_india, na.rm=TRUE)*100,
         threemorb = weighted.mean(three_morb, sworld_weight_india, na.rm=TRUE)*100,
         fourmorb = weighted.mean(four_morb, sworld_weight_india, na.rm=TRUE)*100,
         fivemorb = weighted.mean(five_morb, sworld_weight_india, na.rm=TRUE)*100,
         sixmorb =weighted.mean(six_morb, sworld_weight_india, na.rm=TRUE)*100,
         sevenmorb = weighted.mean(seven_morb, sworld_weight_india, na.rm=TRUE)*100,
         eightmorb = weighted.mean(eight_morb, sworld_weight_india, na.rm=TRUE)*100,
         group = 1) %>%
  filter(row_number()==1) %>%
  ungroup() %>% 
  dplyr::select(age, zeromorb, onemorb, twomorb, threemorb, fourmorb, fivemorb, sixmorb, sevenmorb, eightmorb)



#####Aware


stateawarefig <- statemeanmultimorbidrural.dat %>% 
  ggplot()+
 # geom_smooth(aes(y=statemean.dat$onemorb, x=statemean.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidrural.dat$twomorb, x=statemeanmultimorbidrural.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidrural.dat$threemorb, x=statemeanmultimorbidrural.dat$age),span=0.5)+
  #geom_smooth(aes(y=statemeanmultimorbidrural.dat$fourmorb, x=statemeanmultimorbidrural.dat$age),span=0.5)+
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidrural.dat$onemorb ~ statemeanmultimorbidrural.dat$age,span=1)),x=statemeanmultimorbidrural.dat$age), alpha = 1, fill = "firebrick1") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidrural.dat$twomorb ~ statemeanmultimorbidrural.dat$age,span=1)),x=statemeanmultimorbidrural.dat$age), alpha = 1, fill = "firebrick3") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidrural.dat$threemorb ~ statemeanmultimorbidrural.dat$age,span=1)),x=statemeanmultimorbidrural.dat$age), alpha = 1, fill = "firebrick4") +
  geom_ribbon(aes(ymin = 0, ymax = predict(loess(statemeanmultimorbidrural.dat$fourmorb ~ statemeanmultimorbidrural.dat$age,span=1)),x=statemeanmultimorbidrural.dat$age), alpha = 1, fill = "gray23") +
  theme_classic() + 
  labs(x = "Age",
       y = " Patients, in %",
       fill="") +
  theme(axis.text=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=20),
        legend.title = element_blank(),
        #legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines")) + 
  scale_color_brewer(palette="Dark2") +
  scale_y_continuous(breaks = c(10, 20,30,40,50,60,70), limits=c(0, 70)) +
  scale_x_continuous(breaks = c(20,30,40,49), limits=c(15, 49)) +
  coord_fixed(34/70, expand=F)
stateawarefig








```





```{r Upsetr DHS 2+3 Interactions Morbidities RIGHT}



dhs_nomiss <- mutate(dhs_nomiss,
                     Diabetes_Obesity= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$obese==1 ,1,0),
                     Hypertension_Obesity= ifelse(dhs_nomiss$ex_htn_broad_ind==1&  dhs_nomiss$obese==1 ,1,0),
                     Asthma_Obesity= ifelse(dhs_nomiss$asthma==1& dhs_nomiss$obese==1 ,1,0),
                     Anemia_Obesity= ifelse(dhs_nomiss$anemia==1&  dhs_nomiss$obese==1 ,1,0),
Diabetes_Hypertension_Obesity= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1 ,1,0),
Diabetes_Hypertension_Asthma= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$asthma==1 ,1,0),
Diabetes_Hypertension_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$anemia==1 ,1,0),
Diabetes_Obesity_Asthma= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1 ,1,0),
Diabetes_Obesity_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$anemia==1 ,1,0),
Diabetes_Asthma_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$anemia==1 ,1,0),
Hypertension_Obesity_Asthma= ifelse(dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1 ,1,0),
Hypertension_Obesity_Anemia= ifelse(dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$anemia==1 ,1,0),
Hypertension_Asthma_Anemia= ifelse(dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 ,1,0),
Obesity_Asthma_Anemia= ifelse(dhs_nomiss$obese==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 ,1,0),
Diabetes_Hypertension_Obesity_Asthma= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1 ,1,0),
Diabetes_Hypertension_Obesity_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$anemia==1 ,1,0),
Diabetes_Hypertension_Asthma_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 ,1,0),
Diabetes_Obesity_Asthma_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia= ifelse(dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia_Diabetes= ifelse(dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 & dhs_nomiss$ex_diab_broad_ind==1 ,1,0))



###create PSU ID in dhs_nomiss_diabetic_twotreatedcomorb :
dhs_nomiss_interact <- dhs_nomiss %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_interact$psuid <- as.factor(dhs_nomiss_interact$psuid)

###make noNAinpsu

dhs_nomiss_interact_noNAinpsu <- filter(dhs_nomiss_interact, is.na(psu)==F)

summary(dhs_nomiss_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_interact_noNAinpsu$psu)==T)



 svy_all <- dhs_nomiss_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension,
   Diabetes_Obesity,
                     Hypertension_Obesity,
                     Asthma_Obesity,
                     Anemia_Obesity,
  Diabetes_Hypertension_Obesity,
Diabetes_Hypertension_Asthma,
Diabetes_Hypertension_Anemia,
Diabetes_Obesity_Asthma,
Diabetes_Obesity_Anemia,
Diabetes_Asthma_Anemia,
Hypertension_Obesity_Asthma,
Hypertension_Obesity_Anemia,
Hypertension_Asthma_Anemia,
Obesity_Asthma_Anemia))

  
  prevtot2morb <- svy_all %>%
    summarize(Hypertension_Obesity_prop = survey_mean(Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100,
              Asthma_Obesity_prop = survey_mean(Asthma_Obesity, proportion=TRUE, vartype = "ci")*100,
              Anemia_Obesity_prop = survey_mean(Anemia_Obesity, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Obesity_prop = survey_mean(Diabetes_Obesity, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100 ,
               anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_asthma_prop = survey_mean(diabetes_asthma, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             Diabetes_Hypertension_Obesity_prop = survey_mean(Diabetes_Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Asthma_prop = survey_mean(Diabetes_Hypertension_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Anemia_prop = survey_mean(Diabetes_Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Asthma_prop = survey_mean(Diabetes_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Anemia_prop = survey_mean(Diabetes_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Asthma_Anemia_prop = survey_mean(Diabetes_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Asthma_prop = survey_mean(Hypertension_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Anemia_prop = survey_mean(Hypertension_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Asthma_Anemia_prop = survey_mean(Hypertension_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Obesity_Asthma_Anemia_prop = survey_mean(Obesity_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100)
       
       

write.csv(prevtot2morb, "prevalence 2way interactions DHS new.csv")

prevalence.2way.interactions.DHS. <- read.csv("~/Documents/Public Health Files/Public Health/public health/paper multimorbidity/prevalence 2way interactions DHS new.csv")
inter <- prevtot2morb

#prevalence.2way.interactions.DHS.anemiahyp.only. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades #with morbidities/prevalence 2way interactions DHS anemiahyp only .csv")

inter$anemia_diabetes_prop <- as.numeric(inter$anemia_diabetes_prop)
inter$anemia_asthma_prop <- as.numeric(inter$anemia_asthma_prop)
inter$anemia_hypertension_prop <- as.numeric(inter$anemia_hypertension_prop)
inter$diabetes_asthma_prop <- as.numeric(inter$diabetes_asthma_prop)
inter$diabetes_hypertension_prop <- as.numeric(inter$diabetes_hypertension_prop)
inter$asthma_hypertension_prop <- as.numeric(inter$asthma_hypertension_prop)
inter$Diabetes_Obesity_prop <- as.numeric(inter$Diabetes_Obesity_prop)
inter$Hypertension_Obesity_prop <- as.numeric(inter$Hypertension_Obesity_prop)
inter$Asthma_Obesity_prop <- as.numeric(inter$Asthma_Obesity_prop)
inter$Anemia_Obesity_prop <- as.numeric(inter$Anemia_Obesity_prop)
inter$Diabetes_Hypertension_Obesity_prop <- as.numeric(inter$Diabetes_Hypertension_Obesity_prop)
inter$Diabetes_Hypertension_Asthma_prop <- as.numeric(inter$Diabetes_Hypertension_Asthma_prop)
inter$Diabetes_Hypertension_Anemia_prop <- as.numeric(inter$Diabetes_Hypertension_Anemia_prop)
inter$Diabetes_Obesity_Asthma_prop <- as.numeric(inter$Diabetes_Obesity_Asthma_prop)
inter$Diabetes_Obesity_Anemia_prop <- as.numeric(inter$Diabetes_Obesity_Anemia_prop)
inter$Diabetes_Asthma_Anemia_prop <- as.numeric(inter$Diabetes_Asthma_Anemia_prop)
inter$Hypertension_Obesity_Asthma_prop <- as.numeric(inter$Hypertension_Obesity_Asthma_prop)
inter$Hypertension_Obesity_Anemia_prop <- as.numeric(inter$Hypertension_Obesity_Anemia_prop)
inter$Hypertension_Asthma_Anemia_prop <- as.numeric(inter$Hypertension_Asthma_Anemia_prop)
inter$Obesity_Asthma_Anemia_prop <- as.numeric(inter$Obesity_Asthma_Anemia_prop)


#inter2 <- prevalence.2way.interactions.DHS.anemiahyp.only.

mycols <- colors()[c(630, 630, 630, 630, 630, 556,630,556,630,630,630,630,556,556,556,556,556,556,556,556)] 

  Upset <- c('Anemia&Diabetes'= (inter$anemia_diabetes_prop)/100*712822,
  'Anemia&Asthma'= (inter$anemia_asthma_prop)/100*712822,
  'Anemia&Hypertension'= (inter$anemia_hypertension_prop)/100*712822,
  'Diabetes&Asthma'= (inter$diabetes_asthma_prop)/100*712822,
  'Diabetes&Hypertension'= (inter$diabetes_hypertension_prop)/100*712822,
  'Asthma&Hypertension'= (inter$asthma_hypertension_prop)/100*712822,
    'Diabetes&Obesity'= (inter$Diabetes_Obesity_prop)/100*712822,
    'Hypertension&Obesity'= (inter$Hypertension_Obesity_prop)/100*712822,
    'Asthma&Obesity'= (inter$Asthma_Obesity_prop)/100*712822,
    'Anemia&Obesity'= (inter$Anemia_Obesity_prop)/100*712822,
  'Diabetes&Hypertension&Obesity'= (inter$Diabetes_Hypertension_Obesity_prop)/100*712822,
    'Diabetes&Hypertension&Asthma'= (inter$Diabetes_Hypertension_Asthma_prop)/100*712822,
    'Diabetes&Hypertension&Anemia'= (inter$Diabetes_Hypertension_Anemia_prop)/100*712822,
    'Diabetes&Obesity&Asthma'= (inter$Diabetes_Obesity_Asthma_prop)/100*712822,
    'Diabetes&Obesity&Anemia'= (inter$Diabetes_Obesity_Anemia_prop)/100*712822,
    'Diabetes&Asthma&Anemia'= (inter$Diabetes_Asthma_Anemia_prop)/100*712822,
    'Hypertension&Obesity&Asthma'= (inter$Hypertension_Obesity_Asthma_prop)/100*712822,
    'Hypertension&Obesity&Anemia'= (inter$Hypertension_Obesity_Anemia_prop)/100*712822,
    'Hypertension&Asthma&Anemia'= (inter$Hypertension_Asthma_Anemia_prop)/100*712822,
    'Obesity&Asthma&Anemia'= (inter$Obesity_Asthma_Anemia_prop)/100*712822)
  
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=5, mainbar.y.label = "Number of Individuals", main.bar.color=mycols)     
  

  ######## 2way Interaction population estimates
  
  
svy_treated <- dhs_nomiss_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = sworld_weight_india,
                     variables = c(Diabetes_Hypertension, Diabetes_Anemia, Obese_Diabetes, Smoking_Diabetes, sev_Thinness_Diabetes, Hypertension_Anemia, Obese_Hypertension, sev_Thinness_Hypertension, Smoking_Hypertension, Obese_Anemia, Obese_Smoking, Smoking_sev_Thinness, Anemia_sev_Thinness, Anemia_Smoking))
  
  prevtot <- svy_all %>%
    summarize(Diabetes_Hypertension_prop = survey_mean(Diabetes_Hypertension, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Anemia_prop = survey_mean(Diabetes_Anemia, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Diabetes_prop = survey_mean(Obese_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
              Smoking_Diabetes_prop = survey_mean(Smoking_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
              sev_Thinness_Diabetes_prop = survey_mean(sev_Thinness_Diabetes, proportion=TRUE, vartype = "ci")*100 ,
              Hypertension_Anemia_prop = survey_mean(Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Hypertension_prop = survey_mean(Obese_Hypertension, proportion=TRUE, vartype = "ci")*100 ,
              sev_Thinness_Hypertension_prop = survey_mean(sev_Thinness_Hypertension, proportion=TRUE, vartype = "ci")*100 ,
              Smoking_Hypertension_prop = survey_mean(Smoking_Hypertension, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Anemia_prop = survey_mean(Obese_Anemia, proportion=TRUE, vartype = "ci")*100 ,
              Obese_Smoking_prop = survey_mean(Obese_Smoking, proportion=TRUE, vartype = "ci")*100 ,
                 Smoking_sev_Thinness_prop = survey_mean(Smoking_sev_Thinness, proportion=TRUE, vartype = "ci")*100 ,
                 Anemia_sev_Thinness_prop = survey_mean(Anemia_sev_Thinness, proportion=TRUE, vartype = "ci")*100 ,
                 Anemia_Smoking_prop = survey_mean(Anemia_Smoking, proportion=TRUE, vartype = "ci")*100)
write_csv(prevtot, "prev 2 interactions DHS 06 06.csv")  



  ######## all 2+ 3 interactions
  
  heatcols <- hsv(1, 1, seq(1,0,length.out = 29))
  
  Upset <- c('Diabetes&Hypertension' = (length(which(merg$ex_diab_broad_ind==1& merg$ex_htn_broad_ind==1))),
             'Diabetes&Anemia' = (length(which(merg$ex_diab_broad_ind==1& merg$ex_anemia_ind==1))),
             'Obese&Diabetes'= (length(which(merg$ex_diab_broad_ind==1& merg$obese==1))),
             'Smoking&Diabetes'= (length(which(merg$ex_diab_broad_ind==1& merg$smoke==1))),
             'sev_Thinness&Diabetes'= (length(which(merg$ex_diab_broad_ind==1& merg$sev_underweight==1))),
             'Hypertension&Anemia'= (length(which(merg$ex_htn_broad_ind==1& merg$ex_anemia_ind==1))) ,
             'Obese&Hypertension'= (length(which(merg$ex_htn_broad_ind==1& merg$obese==1))),
             'sev_Thinness&Hypertension'= (length(which(merg$ex_htn_broad_ind==1& merg$sev_underweight==1))),
             'Smoking&Hypertension'= (length(which(merg$ex_htn_broad_ind==1& merg$smoke==1))),
             'Obese&Anemia'= (length(which(merg$ex_anemia_ind==1& merg$obese==1))),
             'Obese&Smoking'= (length(which(merg$smoke==1& merg$obese==1))),
             'Smoking&sev_Thinness'= (length(which(merg$sev_underweight==1& merg$smoke==1))),
             'Anemia&sev_Thinness'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1))),
             'Anemia&Smoking'= (length(which(merg$smoke==1& merg$ex_anemia_ind==1))),
             'Smoking&Hypertension&Diabetes'= (length(which(merg$ex_htn_broad_ind==1& merg$smoke==1 & merg$ex_diab_broad_ind==1))),
             'Smoking&Hypertension&Obese'= (length(which(merg$ex_htn_broad_ind==1& merg$smoke==1 & merg$obese==1))),
             'Smoking&Hypertension&anemia'= (length(which(merg$ex_htn_broad_ind==1& merg$smoke==1 & merg$ex_anemia_ind==1))),
             'Smoking&Hypertension&sev_Thinness'= (length(which(merg$ex_htn_broad_ind==1& merg$smoke==1 & merg$sev_underweight==1))),
             'Smoking&Diabetes&Obese'= (length(which(merg$ex_diab_broad_ind==1& merg$smoke==1 & merg$obese==1))),
             'Smoking&Diabetes&Anemia'= (length(which(merg$ex_diab_broad_ind==1& merg$smoke==1 & merg$ex_anemia_ind==1))),
             'Smoking&Diabetes&sev_Thinness'= (length(which(merg$ex_diab_broad_ind==1& merg$smoke==1 & merg$sev_underweight==1))),
             'Obese&Diabetes&Hypertension'= (length(which(merg$ex_diab_broad_ind==1& merg$obese==1 & merg$ex_htn_broad_ind==1))),
             'Obese&Diabetes&Anemia'= (length(which(merg$ex_diab_broad_ind==1& merg$obese==1 & merg$ex_anemia_ind==1))),
             'Obese&Anemia&Smoking'= (length(which(merg$ex_anemia_ind==1& merg$obese==1 & merg$smoke==1))),
             'Obese&Anemia&Hypertension'= (length(which(merg$ex_anemia_ind==1& merg$obese==1 & merg$ex_htn_broad_ind==1))),
             'Anemia&sev_Thinness&Diabetes'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$ex_diab_broad_ind==1))),
             'Anemia&sev_Thinness&Smoking'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$smoke==1))),
             'Anemia&sev_Thinness&Hypertension'= (length(which(merg$sev_underweight==1& merg$ex_anemia_ind==1 & merg$ex_htn_broad_ind==1))),
             'Hypertension&Diabetes&Anemia'= (length(which(merg$ex_diab_broad_ind==1& merg$ex_htn_broad_ind==1 & merg$ex_anemia_ind==1))),
             'Hypertension&Diabetes&sev_Thinness'= (length(which(merg$ex_diab_broad_ind==1& merg$ex_htn_broad_ind==1 & merg$sev_underweight==1))))
             
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, main.bar.color=heatcols)
  
  
  
##### 2 and 3 way randomized  
  


     svy_all <- merg %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c(  anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension,
  anemia_diabetes_asthma,
  anemia_diabetes_hypertension,
  anemia_asthma_hypertension,
  diabetes_asthma_hypertension))
     
       prevtot3random <- svy_all %>%
    summarize(anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100 ,
              anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_asthma_prop = survey_mean(diabetes_asthma, proportion=TRUE, vartype = "ci")*100 ,
             diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              anemia_diabetes_asthma_prop = survey_mean(anemia_diabetes_asthma, proportion=TRUE, vartype = "ci")*100 ,
              anemia_diabetes_hypertension_prop = survey_mean(anemia_diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              anemia_asthma_hypertension_prop = survey_mean(anemia_asthma_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_asthma_hypertension_prop = survey_mean(diabetes_asthma_hypertension, proportion=TRUE, vartype = "ci")*100 )
              
      
write.csv(prevtot3random, "prevalence 3way interactions DHS morbidities .csv")

   
  
  
  
  
```



```{r Upsetr DHS 2+3 Interactions Morbidities RIGHT Men vs women}


####MEN

  dhs_nomiss_men <- filter(dhs_nomiss, sex==0)

dhs_nomiss_men <- mutate(dhs_nomiss_men,
                     Diabetes_Obesity= ifelse(dhs_nomiss_men$ex_diab_broad_ind==1& dhs_nomiss_men$obese==1 ,1,0),
                     Hypertension_Obesity= ifelse(dhs_nomiss_men$ex_htn_broad_ind==1&  dhs_nomiss_men$obese==1 ,1,0),
                     Asthma_Obesity= ifelse(dhs_nomiss_men$asthma==1& dhs_nomiss_men$obese==1 ,1,0),
                     Anemia_Obesity= ifelse(dhs_nomiss_men$anemia==1&  dhs_nomiss_men$obese==1 ,1,0),
Diabetes_Hypertension_Obesity= ifelse(dhs_nomiss_men$ex_diab_broad_ind==1& dhs_nomiss_men$ex_htn_broad_ind==1& dhs_nomiss_men$obese==1 ,1,0),
Diabetes_Hypertension_Asthma= ifelse(dhs_nomiss_men$ex_diab_broad_ind==1& dhs_nomiss_men$ex_htn_broad_ind==1& dhs_nomiss_men$asthma==1 ,1,0),
Diabetes_Hypertension_Anemia= ifelse(dhs_nomiss_men$ex_diab_broad_ind==1& dhs_nomiss_men$ex_htn_broad_ind==1& dhs_nomiss_men$anemia==1 ,1,0),
Diabetes_Obesity_Asthma= ifelse(dhs_nomiss_men$ex_diab_broad_ind==1& dhs_nomiss_men$obese==1& dhs_nomiss_men$asthma==1 ,1,0),
Diabetes_Obesity_Anemia= ifelse(dhs_nomiss_men$ex_diab_broad_ind==1& dhs_nomiss_men$obese==1& dhs_nomiss_men$anemia==1 ,1,0),
Diabetes_Asthma_Anemia= ifelse(dhs_nomiss_men$ex_diab_broad_ind==1& dhs_nomiss_men$obese==1& dhs_nomiss_men$anemia==1 ,1,0),
Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_men$ex_htn_broad_ind==1& dhs_nomiss_men$obese==1& dhs_nomiss_men$asthma==1 ,1,0),
Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_men$ex_htn_broad_ind==1& dhs_nomiss_men$obese==1& dhs_nomiss_men$anemia==1 ,1,0),
Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_men$ex_htn_broad_ind==1& dhs_nomiss_men$asthma==1& dhs_nomiss_men$anemia==1 ,1,0),
Obesity_Asthma_Anemia= ifelse(dhs_nomiss_men$obese==1& dhs_nomiss_men$asthma==1& dhs_nomiss_men$anemia==1 ,1,0),
Diabetes_Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_men$ex_diab_broad_ind==1& dhs_nomiss_men$ex_htn_broad_ind==1& dhs_nomiss_men$obese==1& dhs_nomiss_men$asthma==1 ,1,0),
Diabetes_Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_men$ex_diab_broad_ind==1& dhs_nomiss_men$ex_htn_broad_ind==1& dhs_nomiss_men$obese==1& dhs_nomiss_men$anemia==1 ,1,0),
Diabetes_Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_men$ex_diab_broad_ind==1& dhs_nomiss_men$ex_htn_broad_ind==1& dhs_nomiss_men$asthma==1& dhs_nomiss_men$anemia==1 ,1,0),
Diabetes_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_men$ex_diab_broad_ind==1& dhs_nomiss_men$obese==1& dhs_nomiss_men$asthma==1& dhs_nomiss_men$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_men$ex_htn_broad_ind==1& dhs_nomiss_men$obese==1& dhs_nomiss_men$asthma==1& dhs_nomiss_men$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia_Diabetes= ifelse(dhs_nomiss_men$ex_htn_broad_ind==1& dhs_nomiss_men$obese==1& dhs_nomiss_men$asthma==1& dhs_nomiss_men$anemia==1 & dhs_nomiss_men$ex_diab_broad_ind==1 ,1,0))



###create PSU ID in dhs_nomiss_men_diabetic_twotreatedcomorb :
dhs_nomiss_men_interact <- dhs_nomiss_men %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_men_interact$psuid <- as.factor(dhs_nomiss_men_interact$psuid)

###make noNAinpsu

dhs_nomiss_men_interact_noNAinpsu <- filter(dhs_nomiss_men_interact, is.na(psu)==F)

summary(dhs_nomiss_men_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_men_interact_noNAinpsu$psu)==T)



 svy_all <- dhs_nomiss_men_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension,
   Diabetes_Obesity,
                     Hypertension_Obesity,
                     Asthma_Obesity,
                     Anemia_Obesity,
  Diabetes_Hypertension_Obesity,
Diabetes_Hypertension_Asthma,
Diabetes_Hypertension_Anemia,
Diabetes_Obesity_Asthma,
Diabetes_Obesity_Anemia,
Diabetes_Asthma_Anemia,
Hypertension_Obesity_Asthma,
Hypertension_Obesity_Anemia,
Hypertension_Asthma_Anemia,
Obesity_Asthma_Anemia))

  
  prevtot2morb <- svy_all %>%
    summarize(Hypertension_Obesity_prop = survey_mean(Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100,
              Asthma_Obesity_prop = survey_mean(Asthma_Obesity, proportion=TRUE, vartype = "ci")*100,
              Anemia_Obesity_prop = survey_mean(Anemia_Obesity, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Obesity_prop = survey_mean(Diabetes_Obesity, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100 ,
               anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_asthma_prop = survey_mean(diabetes_asthma, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             Diabetes_Hypertension_Obesity_prop = survey_mean(Diabetes_Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Asthma_prop = survey_mean(Diabetes_Hypertension_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Anemia_prop = survey_mean(Diabetes_Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Asthma_prop = survey_mean(Diabetes_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Anemia_prop = survey_mean(Diabetes_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Asthma_Anemia_prop = survey_mean(Diabetes_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Asthma_prop = survey_mean(Hypertension_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Anemia_prop = survey_mean(Hypertension_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Asthma_Anemia_prop = survey_mean(Hypertension_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Obesity_Asthma_Anemia_prop = survey_mean(Obesity_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100)
       
       

write.csv(prevtot2morb, "prevalence 2way interactions DHS men.csv")

prevtot2morb  <- read.csv("~/Documents/Public Health Files/Public Health/Paper/paper multimorbidity/prevalence 2way interactions DHS men.csv")
inter <- prevtot2morb

#prevalence.2way.interactions.DHS.anemiahyp.only. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades #with morbidities/prevalence 2way interactions DHS anemiahyp only .csv")

inter$anemia_diabetes_prop <- as.numeric(inter$anemia_diabetes_prop)
inter$anemia_asthma_prop <- as.numeric(inter$anemia_asthma_prop)
inter$anemia_hypertension_prop <- as.numeric(inter$anemia_hypertension_prop)
inter$diabetes_asthma_prop <- as.numeric(inter$diabetes_asthma_prop)
inter$diabetes_hypertension_prop <- as.numeric(inter$diabetes_hypertension_prop)
inter$asthma_hypertension_prop <- as.numeric(inter$asthma_hypertension_prop)
inter$Diabetes_Obesity_prop <- as.numeric(inter$Diabetes_Obesity_prop)
inter$Hypertension_Obesity_prop <- as.numeric(inter$Hypertension_Obesity_prop)
inter$Asthma_Obesity_prop <- as.numeric(inter$Asthma_Obesity_prop)
inter$Anemia_Obesity_prop <- as.numeric(inter$Anemia_Obesity_prop)
inter$Diabetes_Hypertension_Obesity_prop <- as.numeric(inter$Diabetes_Hypertension_Obesity_prop)
inter$Diabetes_Hypertension_Asthma_prop <- as.numeric(inter$Diabetes_Hypertension_Asthma_prop)
inter$Diabetes_Hypertension_Anemia_prop <- as.numeric(inter$Diabetes_Hypertension_Anemia_prop)
inter$Diabetes_Obesity_Asthma_prop <- as.numeric(inter$Diabetes_Obesity_Asthma_prop)
inter$Diabetes_Obesity_Anemia_prop <- as.numeric(inter$Diabetes_Obesity_Anemia_prop)
inter$Diabetes_Asthma_Anemia_prop <- as.numeric(inter$Diabetes_Asthma_Anemia_prop)
inter$Hypertension_Obesity_Asthma_prop <- as.numeric(inter$Hypertension_Obesity_Asthma_prop)
inter$Hypertension_Obesity_Anemia_prop <- as.numeric(inter$Hypertension_Obesity_Anemia_prop)
inter$Hypertension_Asthma_Anemia_prop <- as.numeric(inter$Hypertension_Asthma_Anemia_prop)
inter$Obesity_Asthma_Anemia_prop <- as.numeric(inter$Obesity_Asthma_Anemia_prop)


#inter2 <- prevalence.2way.interactions.DHS.anemiahyp.only.

mycols <- colors()[c(630, 630, 630, 630, 556, 630,630,630,630,630,556,630,556,556,556,556,556,556,556,556)] 

  Upset <- c('Anemia&Diabetes'= (inter$anemia_diabetes_prop)/100*712822,
  'Anemia&Asthma'= (inter$anemia_asthma_prop)/100*712822,
  'Anemia&Hypertension'= (inter$anemia_hypertension_prop)/100*712822,
  'Diabetes&Asthma'= (inter$diabetes_asthma_prop)/100*712822,
  'Diabetes&Hypertension'= (inter$diabetes_hypertension_prop)/100*712822,
  'Asthma&Hypertension'= (inter$asthma_hypertension_prop)/100*712822,
    'Diabetes&Obesity'= (inter$Diabetes_Obesity_prop)/100*712822,
    'Hypertension&Obesity'= (inter$Hypertension_Obesity_prop)/100*712822,
    'Asthma&Obesity'= (inter$Asthma_Obesity_prop)/100*712822,
    'Anemia&Obesity'= (inter$Anemia_Obesity_prop)/100*712822,
  'Diabetes&Hypertension&Obesity'= (inter$Diabetes_Hypertension_Obesity_prop)/100*712822,
    'Diabetes&Hypertension&Asthma'= (inter$Diabetes_Hypertension_Asthma_prop)/100*712822,
    'Diabetes&Hypertension&Anemia'= (inter$Diabetes_Hypertension_Anemia_prop)/100*712822,
    'Diabetes&Obesity&Asthma'= (inter$Diabetes_Obesity_Asthma_prop)/100*712822,
    'Diabetes&Obesity&Anemia'= (inter$Diabetes_Obesity_Anemia_prop)/100*712822,
    'Diabetes&Asthma&Anemia'= (inter$Diabetes_Asthma_Anemia_prop)/100*712822,
    'Hypertension&Obesity&Asthma'= (inter$Hypertension_Obesity_Asthma_prop)/100*712822,
    'Hypertension&Obesity&Anemia'= (inter$Hypertension_Obesity_Anemia_prop)/100*712822,
    'Hypertension&Asthma&Anemia'= (inter$Hypertension_Asthma_Anemia_prop)/100*712822,
    'Obesity&Asthma&Anemia'= (inter$Obesity_Asthma_Anemia_prop)/100*712822)
  
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=5, mainbar.y.label = "Number of Individuals", main.bar.color=mycols)     
  

 ####WOMEN
  
  dhs_nomiss_women <- filter(dhs_nomiss, sex==1)
  
  
  dhs_nomiss_women <- mutate(dhs_nomiss_women,
                     Diabetes_Obesity= ifelse(dhs_nomiss_women$ex_diab_broad_ind==1& dhs_nomiss_women$obese==1 ,1,0),
                     Hypertension_Obesity= ifelse(dhs_nomiss_women$ex_htn_broad_ind==1&  dhs_nomiss_women$obese==1 ,1,0),
                     Asthma_Obesity= ifelse(dhs_nomiss_women$asthma==1& dhs_nomiss_women$obese==1 ,1,0),
                     Anemia_Obesity= ifelse(dhs_nomiss_women$anemia==1&  dhs_nomiss_women$obese==1 ,1,0),
Diabetes_Hypertension_Obesity= ifelse(dhs_nomiss_women$ex_diab_broad_ind==1& dhs_nomiss_women$ex_htn_broad_ind==1& dhs_nomiss_women$obese==1 ,1,0),
Diabetes_Hypertension_Asthma= ifelse(dhs_nomiss_women$ex_diab_broad_ind==1& dhs_nomiss_women$ex_htn_broad_ind==1& dhs_nomiss_women$asthma==1 ,1,0),
Diabetes_Hypertension_Anemia= ifelse(dhs_nomiss_women$ex_diab_broad_ind==1& dhs_nomiss_women$ex_htn_broad_ind==1& dhs_nomiss_women$anemia==1 ,1,0),
Diabetes_Obesity_Asthma= ifelse(dhs_nomiss_women$ex_diab_broad_ind==1& dhs_nomiss_women$obese==1& dhs_nomiss_women$asthma==1 ,1,0),
Diabetes_Obesity_Anemia= ifelse(dhs_nomiss_women$ex_diab_broad_ind==1& dhs_nomiss_women$obese==1& dhs_nomiss_women$anemia==1 ,1,0),
Diabetes_Asthma_Anemia= ifelse(dhs_nomiss_women$ex_diab_broad_ind==1& dhs_nomiss_women$obese==1& dhs_nomiss_women$anemia==1 ,1,0),
Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_women$ex_htn_broad_ind==1& dhs_nomiss_women$obese==1& dhs_nomiss_women$asthma==1 ,1,0),
Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_women$ex_htn_broad_ind==1& dhs_nomiss_women$obese==1& dhs_nomiss_women$anemia==1 ,1,0),
Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_women$ex_htn_broad_ind==1& dhs_nomiss_women$asthma==1& dhs_nomiss_women$anemia==1 ,1,0),
Obesity_Asthma_Anemia= ifelse(dhs_nomiss_women$obese==1& dhs_nomiss_women$asthma==1& dhs_nomiss_women$anemia==1 ,1,0),
Diabetes_Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_women$ex_diab_broad_ind==1& dhs_nomiss_women$ex_htn_broad_ind==1& dhs_nomiss_women$obese==1& dhs_nomiss_women$asthma==1 ,1,0),
Diabetes_Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_women$ex_diab_broad_ind==1& dhs_nomiss_women$ex_htn_broad_ind==1& dhs_nomiss_women$obese==1& dhs_nomiss_women$anemia==1 ,1,0),
Diabetes_Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_women$ex_diab_broad_ind==1& dhs_nomiss_women$ex_htn_broad_ind==1& dhs_nomiss_women$asthma==1& dhs_nomiss_women$anemia==1 ,1,0),
Diabetes_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_women$ex_diab_broad_ind==1& dhs_nomiss_women$obese==1& dhs_nomiss_women$asthma==1& dhs_nomiss_women$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_women$ex_htn_broad_ind==1& dhs_nomiss_women$obese==1& dhs_nomiss_women$asthma==1& dhs_nomiss_women$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia_Diabetes= ifelse(dhs_nomiss_women$ex_htn_broad_ind==1& dhs_nomiss_women$obese==1& dhs_nomiss_women$asthma==1& dhs_nomiss_women$anemia==1 & dhs_nomiss_women$ex_diab_broad_ind==1 ,1,0))



###create PSU ID in dhs_nomiss_women_diabetic_twotreatedcomorb :
dhs_nomiss_women_interact <- dhs_nomiss_women %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_women_interact$psuid <- as.factor(dhs_nomiss_women_interact$psuid)

###make noNAinpsu

dhs_nomiss_women_interact_noNAinpsu <- filter(dhs_nomiss_women_interact, is.na(psu)==F)

summary(dhs_nomiss_women_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_women_interact_noNAinpsu$psu)==T)



 svy_all <- dhs_nomiss_women_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension,
   Diabetes_Obesity,
                     Hypertension_Obesity,
                     Asthma_Obesity,
                     Anemia_Obesity,
  Diabetes_Hypertension_Obesity,
Diabetes_Hypertension_Asthma,
Diabetes_Hypertension_Anemia,
Diabetes_Obesity_Asthma,
Diabetes_Obesity_Anemia,
Diabetes_Asthma_Anemia,
Hypertension_Obesity_Asthma,
Hypertension_Obesity_Anemia,
Hypertension_Asthma_Anemia,
Obesity_Asthma_Anemia))

  
  prevtot2morb <- svy_all %>%
    summarize(Hypertension_Obesity_prop = survey_mean(Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100,
              Asthma_Obesity_prop = survey_mean(Asthma_Obesity, proportion=TRUE, vartype = "ci")*100,
              Anemia_Obesity_prop = survey_mean(Anemia_Obesity, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Obesity_prop = survey_mean(Diabetes_Obesity, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100 ,
               anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_asthma_prop = survey_mean(diabetes_asthma, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             Diabetes_Hypertension_Obesity_prop = survey_mean(Diabetes_Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Asthma_prop = survey_mean(Diabetes_Hypertension_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Anemia_prop = survey_mean(Diabetes_Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Asthma_prop = survey_mean(Diabetes_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Anemia_prop = survey_mean(Diabetes_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Asthma_Anemia_prop = survey_mean(Diabetes_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Asthma_prop = survey_mean(Hypertension_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Anemia_prop = survey_mean(Hypertension_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Asthma_Anemia_prop = survey_mean(Hypertension_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Obesity_Asthma_Anemia_prop = survey_mean(Obesity_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100)
       
       

write.csv(prevtot2morb, "prevalence 2way interactions DHS women.csv")

prevalence.2way.interactions.DHS. <- read.csv("~/Documents/Public Health Files/Public Health/public health/paper multimorbidity/prevalence 2way interactions DHS women.csv")
inter <- prevtot2morb

#prevalence.2way.interactions.DHS.anemiahyp.only. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades #with morbidities/prevalence 2way interactions DHS anemiahyp only .csv")

inter$anemia_diabetes_prop <- as.numeric(inter$anemia_diabetes_prop)
inter$anemia_asthma_prop <- as.numeric(inter$anemia_asthma_prop)
inter$anemia_hypertension_prop <- as.numeric(inter$anemia_hypertension_prop)
inter$diabetes_asthma_prop <- as.numeric(inter$diabetes_asthma_prop)
inter$diabetes_hypertension_prop <- as.numeric(inter$diabetes_hypertension_prop)
inter$asthma_hypertension_prop <- as.numeric(inter$asthma_hypertension_prop)
inter$Diabetes_Obesity_prop <- as.numeric(inter$Diabetes_Obesity_prop)
inter$Hypertension_Obesity_prop <- as.numeric(inter$Hypertension_Obesity_prop)
inter$Asthma_Obesity_prop <- as.numeric(inter$Asthma_Obesity_prop)
inter$Anemia_Obesity_prop <- as.numeric(inter$Anemia_Obesity_prop)
inter$Diabetes_Hypertension_Obesity_prop <- as.numeric(inter$Diabetes_Hypertension_Obesity_prop)
inter$Diabetes_Hypertension_Asthma_prop <- as.numeric(inter$Diabetes_Hypertension_Asthma_prop)
inter$Diabetes_Hypertension_Anemia_prop <- as.numeric(inter$Diabetes_Hypertension_Anemia_prop)
inter$Diabetes_Obesity_Asthma_prop <- as.numeric(inter$Diabetes_Obesity_Asthma_prop)
inter$Diabetes_Obesity_Anemia_prop <- as.numeric(inter$Diabetes_Obesity_Anemia_prop)
inter$Diabetes_Asthma_Anemia_prop <- as.numeric(inter$Diabetes_Asthma_Anemia_prop)
inter$Hypertension_Obesity_Asthma_prop <- as.numeric(inter$Hypertension_Obesity_Asthma_prop)
inter$Hypertension_Obesity_Anemia_prop <- as.numeric(inter$Hypertension_Obesity_Anemia_prop)
inter$Hypertension_Asthma_Anemia_prop <- as.numeric(inter$Hypertension_Asthma_Anemia_prop)
inter$Obesity_Asthma_Anemia_prop <- as.numeric(inter$Obesity_Asthma_Anemia_prop)


#inter2 <- prevalence.2way.interactions.DHS.anemiahyp.only.

mycols <- colors()[c(630, 630, 630, 630, 630, 630,556,630,630,556,630,630,556,556,556,556,556,556,556,556)] 

  Upset <- c('Anemia&Diabetes'= (inter$anemia_diabetes_prop)/100*712822,
  'Anemia&Asthma'= (inter$anemia_asthma_prop)/100*712822,
  'Anemia&Hypertension'= (inter$anemia_hypertension_prop)/100*712822,
  'Diabetes&Asthma'= (inter$diabetes_asthma_prop)/100*712822,
  'Diabetes&Hypertension'= (inter$diabetes_hypertension_prop)/100*712822,
  'Asthma&Hypertension'= (inter$asthma_hypertension_prop)/100*712822,
    'Diabetes&Obesity'= (inter$Diabetes_Obesity_prop)/100*712822,
    'Hypertension&Obesity'= (inter$Hypertension_Obesity_prop)/100*712822,
    'Asthma&Obesity'= (inter$Asthma_Obesity_prop)/100*712822,
    'Anemia&Obesity'= (inter$Anemia_Obesity_prop)/100*712822,
  'Diabetes&Hypertension&Obesity'= (inter$Diabetes_Hypertension_Obesity_prop)/100*712822,
    'Diabetes&Hypertension&Asthma'= (inter$Diabetes_Hypertension_Asthma_prop)/100*712822,
    'Diabetes&Hypertension&Anemia'= (inter$Diabetes_Hypertension_Anemia_prop)/100*712822,
    'Diabetes&Obesity&Asthma'= (inter$Diabetes_Obesity_Asthma_prop)/100*712822,
    'Diabetes&Obesity&Anemia'= (inter$Diabetes_Obesity_Anemia_prop)/100*712822,
    'Diabetes&Asthma&Anemia'= (inter$Diabetes_Asthma_Anemia_prop)/100*712822,
    'Hypertension&Obesity&Asthma'= (inter$Hypertension_Obesity_Asthma_prop)/100*712822,
    'Hypertension&Obesity&Anemia'= (inter$Hypertension_Obesity_Anemia_prop)/100*712822,
    'Hypertension&Asthma&Anemia'= (inter$Hypertension_Asthma_Anemia_prop)/100*712822,
    'Obesity&Asthma&Anemia'= (inter$Obesity_Asthma_Anemia_prop)/100*712822)
  
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=5, mainbar.y.label = "Number of Individuals", main.bar.color=mycols)     
  

 
  
```


```{r Upsetr DHS 2+3 Interactions Morbidities RIGHT rural vs urban}



####urban

  dhs_nomiss_urban <- filter(dhs_nomiss, urban==1)

dhs_nomiss_urban <- mutate(dhs_nomiss_urban,
                     Diabetes_Obesity= ifelse(dhs_nomiss_urban$ex_diab_broad_ind==1& dhs_nomiss_urban$obese==1 ,1,0),
                     Hypertension_Obesity= ifelse(dhs_nomiss_urban$ex_htn_broad_ind==1&  dhs_nomiss_urban$obese==1 ,1,0),
                     Asthma_Obesity= ifelse(dhs_nomiss_urban$asthma==1& dhs_nomiss_urban$obese==1 ,1,0),
                     Anemia_Obesity= ifelse(dhs_nomiss_urban$anemia==1&  dhs_nomiss_urban$obese==1 ,1,0),
Diabetes_Hypertension_Obesity= ifelse(dhs_nomiss_urban$ex_diab_broad_ind==1& dhs_nomiss_urban$ex_htn_broad_ind==1& dhs_nomiss_urban$obese==1 ,1,0),
Diabetes_Hypertension_Asthma= ifelse(dhs_nomiss_urban$ex_diab_broad_ind==1& dhs_nomiss_urban$ex_htn_broad_ind==1& dhs_nomiss_urban$asthma==1 ,1,0),
Diabetes_Hypertension_Anemia= ifelse(dhs_nomiss_urban$ex_diab_broad_ind==1& dhs_nomiss_urban$ex_htn_broad_ind==1& dhs_nomiss_urban$anemia==1 ,1,0),
Diabetes_Obesity_Asthma= ifelse(dhs_nomiss_urban$ex_diab_broad_ind==1& dhs_nomiss_urban$obese==1& dhs_nomiss_urban$asthma==1 ,1,0),
Diabetes_Obesity_Anemia= ifelse(dhs_nomiss_urban$ex_diab_broad_ind==1& dhs_nomiss_urban$obese==1& dhs_nomiss_urban$anemia==1 ,1,0),
Diabetes_Asthma_Anemia= ifelse(dhs_nomiss_urban$ex_diab_broad_ind==1& dhs_nomiss_urban$obese==1& dhs_nomiss_urban$anemia==1 ,1,0),
Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_urban$ex_htn_broad_ind==1& dhs_nomiss_urban$obese==1& dhs_nomiss_urban$asthma==1 ,1,0),
Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_urban$ex_htn_broad_ind==1& dhs_nomiss_urban$obese==1& dhs_nomiss_urban$anemia==1 ,1,0),
Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_urban$ex_htn_broad_ind==1& dhs_nomiss_urban$asthma==1& dhs_nomiss_urban$anemia==1 ,1,0),
Obesity_Asthma_Anemia= ifelse(dhs_nomiss_urban$obese==1& dhs_nomiss_urban$asthma==1& dhs_nomiss_urban$anemia==1 ,1,0),
Diabetes_Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_urban$ex_diab_broad_ind==1& dhs_nomiss_urban$ex_htn_broad_ind==1& dhs_nomiss_urban$obese==1& dhs_nomiss_urban$asthma==1 ,1,0),
Diabetes_Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_urban$ex_diab_broad_ind==1& dhs_nomiss_urban$ex_htn_broad_ind==1& dhs_nomiss_urban$obese==1& dhs_nomiss_urban$anemia==1 ,1,0),
Diabetes_Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_urban$ex_diab_broad_ind==1& dhs_nomiss_urban$ex_htn_broad_ind==1& dhs_nomiss_urban$asthma==1& dhs_nomiss_urban$anemia==1 ,1,0),
Diabetes_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_urban$ex_diab_broad_ind==1& dhs_nomiss_urban$obese==1& dhs_nomiss_urban$asthma==1& dhs_nomiss_urban$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_urban$ex_htn_broad_ind==1& dhs_nomiss_urban$obese==1& dhs_nomiss_urban$asthma==1& dhs_nomiss_urban$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia_Diabetes= ifelse(dhs_nomiss_urban$ex_htn_broad_ind==1& dhs_nomiss_urban$obese==1& dhs_nomiss_urban$asthma==1& dhs_nomiss_urban$anemia==1 & dhs_nomiss_urban$ex_diab_broad_ind==1 ,1,0))



###create PSU ID in dhs_nomiss_urban_diabetic_twotreatedcomorb :
dhs_nomiss_urban_interact <- dhs_nomiss_urban %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_urban_interact$psuid <- as.factor(dhs_nomiss_urban_interact$psuid)

###make noNAinpsu

dhs_nomiss_urban_interact_noNAinpsu <- filter(dhs_nomiss_urban_interact, is.na(psu)==F)

summary(dhs_nomiss_urban_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_urban_interact_noNAinpsu$psu)==T)



 svy_all <- dhs_nomiss_urban_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension,
   Diabetes_Obesity,
                     Hypertension_Obesity,
                     Asthma_Obesity,
                     Anemia_Obesity,
  Diabetes_Hypertension_Obesity,
Diabetes_Hypertension_Asthma,
Diabetes_Hypertension_Anemia,
Diabetes_Obesity_Asthma,
Diabetes_Obesity_Anemia,
Diabetes_Asthma_Anemia,
Hypertension_Obesity_Asthma,
Hypertension_Obesity_Anemia,
Hypertension_Asthma_Anemia,
Obesity_Asthma_Anemia))

  
  prevtot2morb <- svy_all %>%
    summarize(Hypertension_Obesity_prop = survey_mean(Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100,
              Asthma_Obesity_prop = survey_mean(Asthma_Obesity, proportion=TRUE, vartype = "ci")*100,
              Anemia_Obesity_prop = survey_mean(Anemia_Obesity, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Obesity_prop = survey_mean(Diabetes_Obesity, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100 ,
               anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_asthma_prop = survey_mean(diabetes_asthma, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             Diabetes_Hypertension_Obesity_prop = survey_mean(Diabetes_Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Asthma_prop = survey_mean(Diabetes_Hypertension_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Anemia_prop = survey_mean(Diabetes_Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Asthma_prop = survey_mean(Diabetes_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Anemia_prop = survey_mean(Diabetes_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Asthma_Anemia_prop = survey_mean(Diabetes_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Asthma_prop = survey_mean(Hypertension_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Anemia_prop = survey_mean(Hypertension_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Asthma_Anemia_prop = survey_mean(Hypertension_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Obesity_Asthma_Anemia_prop = survey_mean(Obesity_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100)
       
       

write.csv(prevtot2morb, "prevalence 2way interactions DHS urban.csv")

prevalence.2way.interactions.DHS. <- read.csv("~/Documents/Public Health Files/Public Health/public health/paper multimorbidity/prevalence 2way interactions DHS urban.csv")
inter <- prevtot2morb

#prevalence.2way.interactions.DHS.anemiahyp.only. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades #with morbidities/prevalence 2way interactions DHS anemiahyp only .csv")

inter$anemia_diabetes_prop <- as.numeric(inter$anemia_diabetes_prop)
inter$anemia_asthma_prop <- as.numeric(inter$anemia_asthma_prop)
inter$anemia_hypertension_prop <- as.numeric(inter$anemia_hypertension_prop)
inter$diabetes_asthma_prop <- as.numeric(inter$diabetes_asthma_prop)
inter$diabetes_hypertension_prop <- as.numeric(inter$diabetes_hypertension_prop)
inter$asthma_hypertension_prop <- as.numeric(inter$asthma_hypertension_prop)
inter$Diabetes_Obesity_prop <- as.numeric(inter$Diabetes_Obesity_prop)
inter$Hypertension_Obesity_prop <- as.numeric(inter$Hypertension_Obesity_prop)
inter$Asthma_Obesity_prop <- as.numeric(inter$Asthma_Obesity_prop)
inter$Anemia_Obesity_prop <- as.numeric(inter$Anemia_Obesity_prop)
inter$Diabetes_Hypertension_Obesity_prop <- as.numeric(inter$Diabetes_Hypertension_Obesity_prop)
inter$Diabetes_Hypertension_Asthma_prop <- as.numeric(inter$Diabetes_Hypertension_Asthma_prop)
inter$Diabetes_Hypertension_Anemia_prop <- as.numeric(inter$Diabetes_Hypertension_Anemia_prop)
inter$Diabetes_Obesity_Asthma_prop <- as.numeric(inter$Diabetes_Obesity_Asthma_prop)
inter$Diabetes_Obesity_Anemia_prop <- as.numeric(inter$Diabetes_Obesity_Anemia_prop)
inter$Diabetes_Asthma_Anemia_prop <- as.numeric(inter$Diabetes_Asthma_Anemia_prop)
inter$Hypertension_Obesity_Asthma_prop <- as.numeric(inter$Hypertension_Obesity_Asthma_prop)
inter$Hypertension_Obesity_Anemia_prop <- as.numeric(inter$Hypertension_Obesity_Anemia_prop)
inter$Hypertension_Asthma_Anemia_prop <- as.numeric(inter$Hypertension_Asthma_Anemia_prop)
inter$Obesity_Asthma_Anemia_prop <- as.numeric(inter$Obesity_Asthma_Anemia_prop)


#inter2 <- prevalence.2way.interactions.DHS.anemiahyp.only.

mycols <- colors()[c(630, 630, 630, 630, 630, 556,630,630,556,630,630,630,556,556,556,556,556,556,556,556)] 

  Upset <- c('Anemia&Diabetes'= (inter$anemia_diabetes_prop)/100*712822,
  'Anemia&Asthma'= (inter$anemia_asthma_prop)/100*712822,
  'Anemia&Hypertension'= (inter$anemia_hypertension_prop)/100*712822,
  'Diabetes&Asthma'= (inter$diabetes_asthma_prop)/100*712822,
  'Diabetes&Hypertension'= (inter$diabetes_hypertension_prop)/100*712822,
  'Asthma&Hypertension'= (inter$asthma_hypertension_prop)/100*712822,
    'Diabetes&Obesity'= (inter$Diabetes_Obesity_prop)/100*712822,
    'Hypertension&Obesity'= (inter$Hypertension_Obesity_prop)/100*712822,
    'Asthma&Obesity'= (inter$Asthma_Obesity_prop)/100*712822,
    'Anemia&Obesity'= (inter$Anemia_Obesity_prop)/100*712822,
  'Diabetes&Hypertension&Obesity'= (inter$Diabetes_Hypertension_Obesity_prop)/100*712822,
    'Diabetes&Hypertension&Asthma'= (inter$Diabetes_Hypertension_Asthma_prop)/100*712822,
    'Diabetes&Hypertension&Anemia'= (inter$Diabetes_Hypertension_Anemia_prop)/100*712822,
    'Diabetes&Obesity&Asthma'= (inter$Diabetes_Obesity_Asthma_prop)/100*712822,
    'Diabetes&Obesity&Anemia'= (inter$Diabetes_Obesity_Anemia_prop)/100*712822,
    'Diabetes&Asthma&Anemia'= (inter$Diabetes_Asthma_Anemia_prop)/100*712822,
    'Hypertension&Obesity&Asthma'= (inter$Hypertension_Obesity_Asthma_prop)/100*712822,
    'Hypertension&Obesity&Anemia'= (inter$Hypertension_Obesity_Anemia_prop)/100*712822,
    'Hypertension&Asthma&Anemia'= (inter$Hypertension_Asthma_Anemia_prop)/100*712822,
    'Obesity&Asthma&Anemia'= (inter$Obesity_Asthma_Anemia_prop)/100*712822)
  
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=5, mainbar.y.label = "Number of Individuals", main.bar.color=mycols)     
  

 ####rural
  
  dhs_nomiss_rural <- filter(dhs_nomiss, urban==0)
  
  
  dhs_nomiss_rural <- mutate(dhs_nomiss_rural,
                     Diabetes_Obesity= ifelse(dhs_nomiss_rural$ex_diab_broad_ind==1& dhs_nomiss_rural$obese==1 ,1,0),
                     Hypertension_Obesity= ifelse(dhs_nomiss_rural$ex_htn_broad_ind==1&  dhs_nomiss_rural$obese==1 ,1,0),
                     Asthma_Obesity= ifelse(dhs_nomiss_rural$asthma==1& dhs_nomiss_rural$obese==1 ,1,0),
                     Anemia_Obesity= ifelse(dhs_nomiss_rural$anemia==1&  dhs_nomiss_rural$obese==1 ,1,0),
Diabetes_Hypertension_Obesity= ifelse(dhs_nomiss_rural$ex_diab_broad_ind==1& dhs_nomiss_rural$ex_htn_broad_ind==1& dhs_nomiss_rural$obese==1 ,1,0),
Diabetes_Hypertension_Asthma= ifelse(dhs_nomiss_rural$ex_diab_broad_ind==1& dhs_nomiss_rural$ex_htn_broad_ind==1& dhs_nomiss_rural$asthma==1 ,1,0),
Diabetes_Hypertension_Anemia= ifelse(dhs_nomiss_rural$ex_diab_broad_ind==1& dhs_nomiss_rural$ex_htn_broad_ind==1& dhs_nomiss_rural$anemia==1 ,1,0),
Diabetes_Obesity_Asthma= ifelse(dhs_nomiss_rural$ex_diab_broad_ind==1& dhs_nomiss_rural$obese==1& dhs_nomiss_rural$asthma==1 ,1,0),
Diabetes_Obesity_Anemia= ifelse(dhs_nomiss_rural$ex_diab_broad_ind==1& dhs_nomiss_rural$obese==1& dhs_nomiss_rural$anemia==1 ,1,0),
Diabetes_Asthma_Anemia= ifelse(dhs_nomiss_rural$ex_diab_broad_ind==1& dhs_nomiss_rural$obese==1& dhs_nomiss_rural$anemia==1 ,1,0),
Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_rural$ex_htn_broad_ind==1& dhs_nomiss_rural$obese==1& dhs_nomiss_rural$asthma==1 ,1,0),
Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_rural$ex_htn_broad_ind==1& dhs_nomiss_rural$obese==1& dhs_nomiss_rural$anemia==1 ,1,0),
Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_rural$ex_htn_broad_ind==1& dhs_nomiss_rural$asthma==1& dhs_nomiss_rural$anemia==1 ,1,0),
Obesity_Asthma_Anemia= ifelse(dhs_nomiss_rural$obese==1& dhs_nomiss_rural$asthma==1& dhs_nomiss_rural$anemia==1 ,1,0),
Diabetes_Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_rural$ex_diab_broad_ind==1& dhs_nomiss_rural$ex_htn_broad_ind==1& dhs_nomiss_rural$obese==1& dhs_nomiss_rural$asthma==1 ,1,0),
Diabetes_Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_rural$ex_diab_broad_ind==1& dhs_nomiss_rural$ex_htn_broad_ind==1& dhs_nomiss_rural$obese==1& dhs_nomiss_rural$anemia==1 ,1,0),
Diabetes_Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_rural$ex_diab_broad_ind==1& dhs_nomiss_rural$ex_htn_broad_ind==1& dhs_nomiss_rural$asthma==1& dhs_nomiss_rural$anemia==1 ,1,0),
Diabetes_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_rural$ex_diab_broad_ind==1& dhs_nomiss_rural$obese==1& dhs_nomiss_rural$asthma==1& dhs_nomiss_rural$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_rural$ex_htn_broad_ind==1& dhs_nomiss_rural$obese==1& dhs_nomiss_rural$asthma==1& dhs_nomiss_rural$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia_Diabetes= ifelse(dhs_nomiss_rural$ex_htn_broad_ind==1& dhs_nomiss_rural$obese==1& dhs_nomiss_rural$asthma==1& dhs_nomiss_rural$anemia==1 & dhs_nomiss_rural$ex_diab_broad_ind==1 ,1,0))



###create PSU ID in dhs_nomiss_rural_diabetic_twotreatedcomorb :
dhs_nomiss_rural_interact <- dhs_nomiss_rural %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_rural_interact$psuid <- as.factor(dhs_nomiss_rural_interact$psuid)

###make noNAinpsu

dhs_nomiss_rural_interact_noNAinpsu <- filter(dhs_nomiss_rural_interact, is.na(psu)==F)

summary(dhs_nomiss_rural_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_rural_interact_noNAinpsu$psu)==T)



 svy_all <- dhs_nomiss_rural_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension,
   Diabetes_Obesity,
                     Hypertension_Obesity,
                     Asthma_Obesity,
                     Anemia_Obesity,
  Diabetes_Hypertension_Obesity,
Diabetes_Hypertension_Asthma,
Diabetes_Hypertension_Anemia,
Diabetes_Obesity_Asthma,
Diabetes_Obesity_Anemia,
Diabetes_Asthma_Anemia,
Hypertension_Obesity_Asthma,
Hypertension_Obesity_Anemia,
Hypertension_Asthma_Anemia,
Obesity_Asthma_Anemia))

  
  prevtot2morb <- svy_all %>%
    summarize(Hypertension_Obesity_prop = survey_mean(Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100,
              Asthma_Obesity_prop = survey_mean(Asthma_Obesity, proportion=TRUE, vartype = "ci")*100,
              Anemia_Obesity_prop = survey_mean(Anemia_Obesity, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Obesity_prop = survey_mean(Diabetes_Obesity, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100 ,
               anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_asthma_prop = survey_mean(diabetes_asthma, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             Diabetes_Hypertension_Obesity_prop = survey_mean(Diabetes_Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Asthma_prop = survey_mean(Diabetes_Hypertension_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Anemia_prop = survey_mean(Diabetes_Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Asthma_prop = survey_mean(Diabetes_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Anemia_prop = survey_mean(Diabetes_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Asthma_Anemia_prop = survey_mean(Diabetes_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Asthma_prop = survey_mean(Hypertension_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Anemia_prop = survey_mean(Hypertension_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Asthma_Anemia_prop = survey_mean(Hypertension_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Obesity_Asthma_Anemia_prop = survey_mean(Obesity_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100)
       
       

write.csv(prevtot2morb, "prevalence 2way interactions DHS rural.csv")

prevalence.2way.interactions.DHS. <- read.csv("~/Documents/Public Health Files/Public Health/public health/paper multimorbidity/prevalence 2way interactions DHS rural.csv")
inter <- prevtot2morb

#prevalence.2way.interactions.DHS.anemiahyp.only. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades #with morbidities/prevalence 2way interactions DHS anemiahyp only .csv")

inter$anemia_diabetes_prop <- as.numeric(inter$anemia_diabetes_prop)
inter$anemia_asthma_prop <- as.numeric(inter$anemia_asthma_prop)
inter$anemia_hypertension_prop <- as.numeric(inter$anemia_hypertension_prop)
inter$diabetes_asthma_prop <- as.numeric(inter$diabetes_asthma_prop)
inter$diabetes_hypertension_prop <- as.numeric(inter$diabetes_hypertension_prop)
inter$asthma_hypertension_prop <- as.numeric(inter$asthma_hypertension_prop)
inter$Diabetes_Obesity_prop <- as.numeric(inter$Diabetes_Obesity_prop)
inter$Hypertension_Obesity_prop <- as.numeric(inter$Hypertension_Obesity_prop)
inter$Asthma_Obesity_prop <- as.numeric(inter$Asthma_Obesity_prop)
inter$Anemia_Obesity_prop <- as.numeric(inter$Anemia_Obesity_prop)
inter$Diabetes_Hypertension_Obesity_prop <- as.numeric(inter$Diabetes_Hypertension_Obesity_prop)
inter$Diabetes_Hypertension_Asthma_prop <- as.numeric(inter$Diabetes_Hypertension_Asthma_prop)
inter$Diabetes_Hypertension_Anemia_prop <- as.numeric(inter$Diabetes_Hypertension_Anemia_prop)
inter$Diabetes_Obesity_Asthma_prop <- as.numeric(inter$Diabetes_Obesity_Asthma_prop)
inter$Diabetes_Obesity_Anemia_prop <- as.numeric(inter$Diabetes_Obesity_Anemia_prop)
inter$Diabetes_Asthma_Anemia_prop <- as.numeric(inter$Diabetes_Asthma_Anemia_prop)
inter$Hypertension_Obesity_Asthma_prop <- as.numeric(inter$Hypertension_Obesity_Asthma_prop)
inter$Hypertension_Obesity_Anemia_prop <- as.numeric(inter$Hypertension_Obesity_Anemia_prop)
inter$Hypertension_Asthma_Anemia_prop <- as.numeric(inter$Hypertension_Asthma_Anemia_prop)
inter$Obesity_Asthma_Anemia_prop <- as.numeric(inter$Obesity_Asthma_Anemia_prop)


#inter2 <- prevalence.2way.interactions.DHS.anemiahyp.only.

mycols <- colors()[c(630, 630, 630, 630, 630, 556,630,630,556,630,630,630,556,556,556,556,556,556,556,556)] 

  Upset <- c('Anemia&Diabetes'= (inter$anemia_diabetes_prop)/100*712822,
  'Anemia&Asthma'= (inter$anemia_asthma_prop)/100*712822,
  'Anemia&Hypertension'= (inter$anemia_hypertension_prop)/100*712822,
  'Diabetes&Asthma'= (inter$diabetes_asthma_prop)/100*712822,
  'Diabetes&Hypertension'= (inter$diabetes_hypertension_prop)/100*712822,
  'Asthma&Hypertension'= (inter$asthma_hypertension_prop)/100*712822,
    'Diabetes&Obesity'= (inter$Diabetes_Obesity_prop)/100*712822,
    'Hypertension&Obesity'= (inter$Hypertension_Obesity_prop)/100*712822,
    'Asthma&Obesity'= (inter$Asthma_Obesity_prop)/100*712822,
    'Anemia&Obesity'= (inter$Anemia_Obesity_prop)/100*712822,
  'Diabetes&Hypertension&Obesity'= (inter$Diabetes_Hypertension_Obesity_prop)/100*712822,
    'Diabetes&Hypertension&Asthma'= (inter$Diabetes_Hypertension_Asthma_prop)/100*712822,
    'Diabetes&Hypertension&Anemia'= (inter$Diabetes_Hypertension_Anemia_prop)/100*712822,
    'Diabetes&Obesity&Asthma'= (inter$Diabetes_Obesity_Asthma_prop)/100*712822,
    'Diabetes&Obesity&Anemia'= (inter$Diabetes_Obesity_Anemia_prop)/100*712822,
    'Diabetes&Asthma&Anemia'= (inter$Diabetes_Asthma_Anemia_prop)/100*712822,
    'Hypertension&Obesity&Asthma'= (inter$Hypertension_Obesity_Asthma_prop)/100*712822,
    'Hypertension&Obesity&Anemia'= (inter$Hypertension_Obesity_Anemia_prop)/100*712822,
    'Hypertension&Asthma&Anemia'= (inter$Hypertension_Asthma_Anemia_prop)/100*712822,
    'Obesity&Asthma&Anemia'= (inter$Obesity_Asthma_Anemia_prop)/100*712822)
  
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=5, mainbar.y.label = "Number of Individuals", main.bar.color=mycols)     
  

```

```{r Upsetr DHS 2+3 Interactions Morbidities RIGHT smoker vs nonsmoker}



####Nonsmoker

  dhs_nomiss_nonsmoker <- filter(dhs_nomiss, tobacco_smoked==0)

dhs_nomiss_nonsmoker <- mutate(dhs_nomiss_nonsmoker,
                     Diabetes_Obesity= ifelse(dhs_nomiss_nonsmoker$ex_diab_broad_ind==1& dhs_nomiss_nonsmoker$obese==1 ,1,0),
                     Hypertension_Obesity= ifelse(dhs_nomiss_nonsmoker$ex_htn_broad_ind==1&  dhs_nomiss_nonsmoker$obese==1 ,1,0),
                     Asthma_Obesity= ifelse(dhs_nomiss_nonsmoker$asthma==1& dhs_nomiss_nonsmoker$obese==1 ,1,0),
                     Anemia_Obesity= ifelse(dhs_nomiss_nonsmoker$anemia==1&  dhs_nomiss_nonsmoker$obese==1 ,1,0),
Diabetes_Hypertension_Obesity= ifelse(dhs_nomiss_nonsmoker$ex_diab_broad_ind==1& dhs_nomiss_nonsmoker$ex_htn_broad_ind==1& dhs_nomiss_nonsmoker$obese==1 ,1,0),
Diabetes_Hypertension_Asthma= ifelse(dhs_nomiss_nonsmoker$ex_diab_broad_ind==1& dhs_nomiss_nonsmoker$ex_htn_broad_ind==1& dhs_nomiss_nonsmoker$asthma==1 ,1,0),
Diabetes_Hypertension_Anemia= ifelse(dhs_nomiss_nonsmoker$ex_diab_broad_ind==1& dhs_nomiss_nonsmoker$ex_htn_broad_ind==1& dhs_nomiss_nonsmoker$anemia==1 ,1,0),
Diabetes_Obesity_Asthma= ifelse(dhs_nomiss_nonsmoker$ex_diab_broad_ind==1& dhs_nomiss_nonsmoker$obese==1& dhs_nomiss_nonsmoker$asthma==1 ,1,0),
Diabetes_Obesity_Anemia= ifelse(dhs_nomiss_nonsmoker$ex_diab_broad_ind==1& dhs_nomiss_nonsmoker$obese==1& dhs_nomiss_nonsmoker$anemia==1 ,1,0),
Diabetes_Asthma_Anemia= ifelse(dhs_nomiss_nonsmoker$ex_diab_broad_ind==1& dhs_nomiss_nonsmoker$obese==1& dhs_nomiss_nonsmoker$anemia==1 ,1,0),
Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_nonsmoker$ex_htn_broad_ind==1& dhs_nomiss_nonsmoker$obese==1& dhs_nomiss_nonsmoker$asthma==1 ,1,0),
Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_nonsmoker$ex_htn_broad_ind==1& dhs_nomiss_nonsmoker$obese==1& dhs_nomiss_nonsmoker$anemia==1 ,1,0),
Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_nonsmoker$ex_htn_broad_ind==1& dhs_nomiss_nonsmoker$asthma==1& dhs_nomiss_nonsmoker$anemia==1 ,1,0),
Obesity_Asthma_Anemia= ifelse(dhs_nomiss_nonsmoker$obese==1& dhs_nomiss_nonsmoker$asthma==1& dhs_nomiss_nonsmoker$anemia==1 ,1,0),
Diabetes_Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_nonsmoker$ex_diab_broad_ind==1& dhs_nomiss_nonsmoker$ex_htn_broad_ind==1& dhs_nomiss_nonsmoker$obese==1& dhs_nomiss_nonsmoker$asthma==1 ,1,0),
Diabetes_Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_nonsmoker$ex_diab_broad_ind==1& dhs_nomiss_nonsmoker$ex_htn_broad_ind==1& dhs_nomiss_nonsmoker$obese==1& dhs_nomiss_nonsmoker$anemia==1 ,1,0),
Diabetes_Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_nonsmoker$ex_diab_broad_ind==1& dhs_nomiss_nonsmoker$ex_htn_broad_ind==1& dhs_nomiss_nonsmoker$asthma==1& dhs_nomiss_nonsmoker$anemia==1 ,1,0),
Diabetes_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_nonsmoker$ex_diab_broad_ind==1& dhs_nomiss_nonsmoker$obese==1& dhs_nomiss_nonsmoker$asthma==1& dhs_nomiss_nonsmoker$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_nonsmoker$ex_htn_broad_ind==1& dhs_nomiss_nonsmoker$obese==1& dhs_nomiss_nonsmoker$asthma==1& dhs_nomiss_nonsmoker$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia_Diabetes= ifelse(dhs_nomiss_nonsmoker$ex_htn_broad_ind==1& dhs_nomiss_nonsmoker$obese==1& dhs_nomiss_nonsmoker$asthma==1& dhs_nomiss_nonsmoker$anemia==1 & dhs_nomiss_nonsmoker$ex_diab_broad_ind==1 ,1,0))



###create PSU ID in dhs_nomiss_nonsmoker_diabetic_twotreatedcomorb :
dhs_nomiss_nonsmoker_interact <- dhs_nomiss_nonsmoker %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_nonsmoker_interact$psuid <- as.factor(dhs_nomiss_nonsmoker_interact$psuid)

###make noNAinpsu

dhs_nomiss_nonsmoker_interact_noNAinpsu <- filter(dhs_nomiss_nonsmoker_interact, is.na(psu)==F)

summary(dhs_nomiss_nonsmoker_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_nonsmoker_interact_noNAinpsu$psu)==T)



 svy_all <- dhs_nomiss_nonsmoker_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension,
   Diabetes_Obesity,
                     Hypertension_Obesity,
                     Asthma_Obesity,
                     Anemia_Obesity,
  Diabetes_Hypertension_Obesity,
Diabetes_Hypertension_Asthma,
Diabetes_Hypertension_Anemia,
Diabetes_Obesity_Asthma,
Diabetes_Obesity_Anemia,
Diabetes_Asthma_Anemia,
Hypertension_Obesity_Asthma,
Hypertension_Obesity_Anemia,
Hypertension_Asthma_Anemia,
Obesity_Asthma_Anemia))

  
  prevtot2morb <- svy_all %>%
    summarize(Hypertension_Obesity_prop = survey_mean(Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100,
              Asthma_Obesity_prop = survey_mean(Asthma_Obesity, proportion=TRUE, vartype = "ci")*100,
              Anemia_Obesity_prop = survey_mean(Anemia_Obesity, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Obesity_prop = survey_mean(Diabetes_Obesity, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100 ,
               anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_asthma_prop = survey_mean(diabetes_asthma, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             Diabetes_Hypertension_Obesity_prop = survey_mean(Diabetes_Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Asthma_prop = survey_mean(Diabetes_Hypertension_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Anemia_prop = survey_mean(Diabetes_Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Asthma_prop = survey_mean(Diabetes_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Anemia_prop = survey_mean(Diabetes_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Asthma_Anemia_prop = survey_mean(Diabetes_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Asthma_prop = survey_mean(Hypertension_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Anemia_prop = survey_mean(Hypertension_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Asthma_Anemia_prop = survey_mean(Hypertension_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Obesity_Asthma_Anemia_prop = survey_mean(Obesity_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100)
       
       

write.csv(prevtot2morb, "prevalence 2way interactions DHS nonsmoker.csv")

prevalence.2way.interactions.DHS. <- read.csv("~/Documents/Public Health Files/Public Health/public health/paper multimorbidity/prevalence 2way interactions DHS nonsmoker.csv")
inter <- prevtot2morb

#prevalence.2way.interactions.DHS.anemiahyp.only. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades #with morbidities/prevalence 2way interactions DHS anemiahyp only .csv")

inter$anemia_diabetes_prop <- as.numeric(inter$anemia_diabetes_prop)
inter$anemia_asthma_prop <- as.numeric(inter$anemia_asthma_prop)
inter$anemia_hypertension_prop <- as.numeric(inter$anemia_hypertension_prop)
inter$diabetes_asthma_prop <- as.numeric(inter$diabetes_asthma_prop)
inter$diabetes_hypertension_prop <- as.numeric(inter$diabetes_hypertension_prop)
inter$asthma_hypertension_prop <- as.numeric(inter$asthma_hypertension_prop)
inter$Diabetes_Obesity_prop <- as.numeric(inter$Diabetes_Obesity_prop)
inter$Hypertension_Obesity_prop <- as.numeric(inter$Hypertension_Obesity_prop)
inter$Asthma_Obesity_prop <- as.numeric(inter$Asthma_Obesity_prop)
inter$Anemia_Obesity_prop <- as.numeric(inter$Anemia_Obesity_prop)
inter$Diabetes_Hypertension_Obesity_prop <- as.numeric(inter$Diabetes_Hypertension_Obesity_prop)
inter$Diabetes_Hypertension_Asthma_prop <- as.numeric(inter$Diabetes_Hypertension_Asthma_prop)
inter$Diabetes_Hypertension_Anemia_prop <- as.numeric(inter$Diabetes_Hypertension_Anemia_prop)
inter$Diabetes_Obesity_Asthma_prop <- as.numeric(inter$Diabetes_Obesity_Asthma_prop)
inter$Diabetes_Obesity_Anemia_prop <- as.numeric(inter$Diabetes_Obesity_Anemia_prop)
inter$Diabetes_Asthma_Anemia_prop <- as.numeric(inter$Diabetes_Asthma_Anemia_prop)
inter$Hypertension_Obesity_Asthma_prop <- as.numeric(inter$Hypertension_Obesity_Asthma_prop)
inter$Hypertension_Obesity_Anemia_prop <- as.numeric(inter$Hypertension_Obesity_Anemia_prop)
inter$Hypertension_Asthma_Anemia_prop <- as.numeric(inter$Hypertension_Asthma_Anemia_prop)
inter$Obesity_Asthma_Anemia_prop <- as.numeric(inter$Obesity_Asthma_Anemia_prop)


#inter2 <- prevalence.2way.interactions.DHS.anemiahyp.only.

mycols <- colors()[c(630, 630, 630, 630, 630, 556,630,630,556,630,630,630,556,556,556,556,556,556,556,556)] 

  Upset <- c('Anemia&Diabetes'= (inter$anemia_diabetes_prop)/100*712822,
  'Anemia&Asthma'= (inter$anemia_asthma_prop)/100*712822,
  'Anemia&Hypertension'= (inter$anemia_hypertension_prop)/100*712822,
  'Diabetes&Asthma'= (inter$diabetes_asthma_prop)/100*712822,
  'Diabetes&Hypertension'= (inter$diabetes_hypertension_prop)/100*712822,
  'Asthma&Hypertension'= (inter$asthma_hypertension_prop)/100*712822,
    'Diabetes&Obesity'= (inter$Diabetes_Obesity_prop)/100*712822,
    'Hypertension&Obesity'= (inter$Hypertension_Obesity_prop)/100*712822,
    'Asthma&Obesity'= (inter$Asthma_Obesity_prop)/100*712822,
    'Anemia&Obesity'= (inter$Anemia_Obesity_prop)/100*712822,
  'Diabetes&Hypertension&Obesity'= (inter$Diabetes_Hypertension_Obesity_prop)/100*712822,
    'Diabetes&Hypertension&Asthma'= (inter$Diabetes_Hypertension_Asthma_prop)/100*712822,
    'Diabetes&Hypertension&Anemia'= (inter$Diabetes_Hypertension_Anemia_prop)/100*712822,
    'Diabetes&Obesity&Asthma'= (inter$Diabetes_Obesity_Asthma_prop)/100*712822,
    'Diabetes&Obesity&Anemia'= (inter$Diabetes_Obesity_Anemia_prop)/100*712822,
    'Diabetes&Asthma&Anemia'= (inter$Diabetes_Asthma_Anemia_prop)/100*712822,
    'Hypertension&Obesity&Asthma'= (inter$Hypertension_Obesity_Asthma_prop)/100*712822,
    'Hypertension&Obesity&Anemia'= (inter$Hypertension_Obesity_Anemia_prop)/100*712822,
    'Hypertension&Asthma&Anemia'= (inter$Hypertension_Asthma_Anemia_prop)/100*712822,
    'Obesity&Asthma&Anemia'= (inter$Obesity_Asthma_Anemia_prop)/100*712822)
  
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=5, mainbar.y.label = "Number of Individuals", main.bar.color=mycols)     
  

 ####Smoker
  
  dhs_nomiss_smoker <- filter(dhs_nomiss,tobacco_smoked==1)
  
  
  dhs_nomiss_smoker <- mutate(dhs_nomiss_smoker,
                     Diabetes_Obesity= ifelse(dhs_nomiss_smoker$ex_diab_broad_ind==1& dhs_nomiss_smoker$obese==1 ,1,0),
                     Hypertension_Obesity= ifelse(dhs_nomiss_smoker$ex_htn_broad_ind==1&  dhs_nomiss_smoker$obese==1 ,1,0),
                     Asthma_Obesity= ifelse(dhs_nomiss_smoker$asthma==1& dhs_nomiss_smoker$obese==1 ,1,0),
                     Anemia_Obesity= ifelse(dhs_nomiss_smoker$anemia==1&  dhs_nomiss_smoker$obese==1 ,1,0),
Diabetes_Hypertension_Obesity= ifelse(dhs_nomiss_smoker$ex_diab_broad_ind==1& dhs_nomiss_smoker$ex_htn_broad_ind==1& dhs_nomiss_smoker$obese==1 ,1,0),
Diabetes_Hypertension_Asthma= ifelse(dhs_nomiss_smoker$ex_diab_broad_ind==1& dhs_nomiss_smoker$ex_htn_broad_ind==1& dhs_nomiss_smoker$asthma==1 ,1,0),
Diabetes_Hypertension_Anemia= ifelse(dhs_nomiss_smoker$ex_diab_broad_ind==1& dhs_nomiss_smoker$ex_htn_broad_ind==1& dhs_nomiss_smoker$anemia==1 ,1,0),
Diabetes_Obesity_Asthma= ifelse(dhs_nomiss_smoker$ex_diab_broad_ind==1& dhs_nomiss_smoker$obese==1& dhs_nomiss_smoker$asthma==1 ,1,0),
Diabetes_Obesity_Anemia= ifelse(dhs_nomiss_smoker$ex_diab_broad_ind==1& dhs_nomiss_smoker$obese==1& dhs_nomiss_smoker$anemia==1 ,1,0),
Diabetes_Asthma_Anemia= ifelse(dhs_nomiss_smoker$ex_diab_broad_ind==1& dhs_nomiss_smoker$obese==1& dhs_nomiss_smoker$anemia==1 ,1,0),
Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_smoker$ex_htn_broad_ind==1& dhs_nomiss_smoker$obese==1& dhs_nomiss_smoker$asthma==1 ,1,0),
Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_smoker$ex_htn_broad_ind==1& dhs_nomiss_smoker$obese==1& dhs_nomiss_smoker$anemia==1 ,1,0),
Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_smoker$ex_htn_broad_ind==1& dhs_nomiss_smoker$asthma==1& dhs_nomiss_smoker$anemia==1 ,1,0),
Obesity_Asthma_Anemia= ifelse(dhs_nomiss_smoker$obese==1& dhs_nomiss_smoker$asthma==1& dhs_nomiss_smoker$anemia==1 ,1,0),
Diabetes_Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_smoker$ex_diab_broad_ind==1& dhs_nomiss_smoker$ex_htn_broad_ind==1& dhs_nomiss_smoker$obese==1& dhs_nomiss_smoker$asthma==1 ,1,0),
Diabetes_Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_smoker$ex_diab_broad_ind==1& dhs_nomiss_smoker$ex_htn_broad_ind==1& dhs_nomiss_smoker$obese==1& dhs_nomiss_smoker$anemia==1 ,1,0),
Diabetes_Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_smoker$ex_diab_broad_ind==1& dhs_nomiss_smoker$ex_htn_broad_ind==1& dhs_nomiss_smoker$asthma==1& dhs_nomiss_smoker$anemia==1 ,1,0),
Diabetes_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_smoker$ex_diab_broad_ind==1& dhs_nomiss_smoker$obese==1& dhs_nomiss_smoker$asthma==1& dhs_nomiss_smoker$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_smoker$ex_htn_broad_ind==1& dhs_nomiss_smoker$obese==1& dhs_nomiss_smoker$asthma==1& dhs_nomiss_smoker$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia_Diabetes= ifelse(dhs_nomiss_smoker$ex_htn_broad_ind==1& dhs_nomiss_smoker$obese==1& dhs_nomiss_smoker$asthma==1& dhs_nomiss_smoker$anemia==1 & dhs_nomiss_smoker$ex_diab_broad_ind==1 ,1,0))



###create PSU ID in dhs_nomiss_smoker_diabetic_twotreatedcomorb :
dhs_nomiss_smoker_interact <- dhs_nomiss_smoker %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_smoker_interact$psuid <- as.factor(dhs_nomiss_smoker_interact$psuid)

###make noNAinpsu

dhs_nomiss_smoker_interact_noNAinpsu <- filter(dhs_nomiss_smoker_interact, is.na(psu)==F)

summary(dhs_nomiss_smoker_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_smoker_interact_noNAinpsu$psu)==T)



 svy_all <- dhs_nomiss_smoker_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension,
   Diabetes_Obesity,
                     Hypertension_Obesity,
                     Asthma_Obesity,
                     Anemia_Obesity,
  Diabetes_Hypertension_Obesity,
Diabetes_Hypertension_Asthma,
Diabetes_Hypertension_Anemia,
Diabetes_Obesity_Asthma,
Diabetes_Obesity_Anemia,
Diabetes_Asthma_Anemia,
Hypertension_Obesity_Asthma,
Hypertension_Obesity_Anemia,
Hypertension_Asthma_Anemia,
Obesity_Asthma_Anemia))

  
  prevtot2morb <- svy_all %>%
    summarize(Hypertension_Obesity_prop = survey_mean(Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100,
              Asthma_Obesity_prop = survey_mean(Asthma_Obesity, proportion=TRUE, vartype = "ci")*100,
              Anemia_Obesity_prop = survey_mean(Anemia_Obesity, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Obesity_prop = survey_mean(Diabetes_Obesity, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100 ,
               anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_asthma_prop = survey_mean(diabetes_asthma, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             Diabetes_Hypertension_Obesity_prop = survey_mean(Diabetes_Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Asthma_prop = survey_mean(Diabetes_Hypertension_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Anemia_prop = survey_mean(Diabetes_Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Asthma_prop = survey_mean(Diabetes_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Anemia_prop = survey_mean(Diabetes_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Asthma_Anemia_prop = survey_mean(Diabetes_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Asthma_prop = survey_mean(Hypertension_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Anemia_prop = survey_mean(Hypertension_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Asthma_Anemia_prop = survey_mean(Hypertension_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Obesity_Asthma_Anemia_prop = survey_mean(Obesity_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100)
       
       

write.csv(prevtot2morb, "prevalence 2way interactions DHS smoker.csv")

prevalence.2way.interactions.DHS. <- read.csv("~/Documents/Public Health Files/Public Health/public health/paper multimorbidity/prevalence 2way interactions DHS smoker.csv")
inter <- prevtot2morb

#prevalence.2way.interactions.DHS.anemiahyp.only. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades #with morbidities/prevalence 2way interactions DHS anemiahyp only .csv")

inter$anemia_diabetes_prop <- as.numeric(inter$anemia_diabetes_prop)
inter$anemia_asthma_prop <- as.numeric(inter$anemia_asthma_prop)
inter$anemia_hypertension_prop <- as.numeric(inter$anemia_hypertension_prop)
inter$diabetes_asthma_prop <- as.numeric(inter$diabetes_asthma_prop)
inter$diabetes_hypertension_prop <- as.numeric(inter$diabetes_hypertension_prop)
inter$asthma_hypertension_prop <- as.numeric(inter$asthma_hypertension_prop)
inter$Diabetes_Obesity_prop <- as.numeric(inter$Diabetes_Obesity_prop)
inter$Hypertension_Obesity_prop <- as.numeric(inter$Hypertension_Obesity_prop)
inter$Asthma_Obesity_prop <- as.numeric(inter$Asthma_Obesity_prop)
inter$Anemia_Obesity_prop <- as.numeric(inter$Anemia_Obesity_prop)
inter$Diabetes_Hypertension_Obesity_prop <- as.numeric(inter$Diabetes_Hypertension_Obesity_prop)
inter$Diabetes_Hypertension_Asthma_prop <- as.numeric(inter$Diabetes_Hypertension_Asthma_prop)
inter$Diabetes_Hypertension_Anemia_prop <- as.numeric(inter$Diabetes_Hypertension_Anemia_prop)
inter$Diabetes_Obesity_Asthma_prop <- as.numeric(inter$Diabetes_Obesity_Asthma_prop)
inter$Diabetes_Obesity_Anemia_prop <- as.numeric(inter$Diabetes_Obesity_Anemia_prop)
inter$Diabetes_Asthma_Anemia_prop <- as.numeric(inter$Diabetes_Asthma_Anemia_prop)
inter$Hypertension_Obesity_Asthma_prop <- as.numeric(inter$Hypertension_Obesity_Asthma_prop)
inter$Hypertension_Obesity_Anemia_prop <- as.numeric(inter$Hypertension_Obesity_Anemia_prop)
inter$Hypertension_Asthma_Anemia_prop <- as.numeric(inter$Hypertension_Asthma_Anemia_prop)
inter$Obesity_Asthma_Anemia_prop <- as.numeric(inter$Obesity_Asthma_Anemia_prop)


#inter2 <- prevalence.2way.interactions.DHS.anemiahyp.only.

mycols <- colors()[c(630, 630, 630, 630, 630, 556,630,630,556,630,630,630,556,556,556,556,556,556,556,556)] 

  Upset <- c('Anemia&Diabetes'= (inter$anemia_diabetes_prop)/100*712822,
  'Anemia&Asthma'= (inter$anemia_asthma_prop)/100*712822,
  'Anemia&Hypertension'= (inter$anemia_hypertension_prop)/100*712822,
  'Diabetes&Asthma'= (inter$diabetes_asthma_prop)/100*712822,
  'Diabetes&Hypertension'= (inter$diabetes_hypertension_prop)/100*712822,
  'Asthma&Hypertension'= (inter$asthma_hypertension_prop)/100*712822,
    'Diabetes&Obesity'= (inter$Diabetes_Obesity_prop)/100*712822,
    'Hypertension&Obesity'= (inter$Hypertension_Obesity_prop)/100*712822,
    'Asthma&Obesity'= (inter$Asthma_Obesity_prop)/100*712822,
    'Anemia&Obesity'= (inter$Anemia_Obesity_prop)/100*712822,
  'Diabetes&Hypertension&Obesity'= (inter$Diabetes_Hypertension_Obesity_prop)/100*712822,
    'Diabetes&Hypertension&Asthma'= (inter$Diabetes_Hypertension_Asthma_prop)/100*712822,
    'Diabetes&Hypertension&Anemia'= (inter$Diabetes_Hypertension_Anemia_prop)/100*712822,
    'Diabetes&Obesity&Asthma'= (inter$Diabetes_Obesity_Asthma_prop)/100*712822,
    'Diabetes&Obesity&Anemia'= (inter$Diabetes_Obesity_Anemia_prop)/100*712822,
    'Diabetes&Asthma&Anemia'= (inter$Diabetes_Asthma_Anemia_prop)/100*712822,
    'Hypertension&Obesity&Asthma'= (inter$Hypertension_Obesity_Asthma_prop)/100*712822,
    'Hypertension&Obesity&Anemia'= (inter$Hypertension_Obesity_Anemia_prop)/100*712822,
    'Hypertension&Asthma&Anemia'= (inter$Hypertension_Asthma_Anemia_prop)/100*712822,
    'Obesity&Asthma&Anemia'= (inter$Obesity_Asthma_Anemia_prop)/100*712822)
  
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=5, mainbar.y.label = "Number of Individuals", main.bar.color=mycols)     
  

  
  
```

```{r Upsetr DHS 2+3 Interactions Morbidities RIGHT by 10 year AGE GROPU}



dhs_nomiss <- mutate(dhs_nomiss,
                     Diabetes_Obesity= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$obese==1 ,1,0),
                     Hypertension_Obesity= ifelse(dhs_nomiss$ex_htn_broad_ind==1&  dhs_nomiss$obese==1 ,1,0),
                     Asthma_Obesity= ifelse(dhs_nomiss$asthma==1& dhs_nomiss$obese==1 ,1,0),
                     Anemia_Obesity= ifelse(dhs_nomiss$anemia==1&  dhs_nomiss$obese==1 ,1,0),
Diabetes_Hypertension_Obesity= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1 ,1,0),
Diabetes_Hypertension_Asthma= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$asthma==1 ,1,0),
Diabetes_Hypertension_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$anemia==1 ,1,0),
Diabetes_Obesity_Asthma= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1 ,1,0),
Diabetes_Obesity_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$anemia==1 ,1,0),
Diabetes_Asthma_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$anemia==1 ,1,0),
Hypertension_Obesity_Asthma= ifelse(dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1 ,1,0),
Hypertension_Obesity_Anemia= ifelse(dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$anemia==1 ,1,0),
Hypertension_Asthma_Anemia= ifelse(dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 ,1,0),
Obesity_Asthma_Anemia= ifelse(dhs_nomiss$obese==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 ,1,0),
Diabetes_Hypertension_Obesity_Asthma= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1 ,1,0),
Diabetes_Hypertension_Obesity_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$anemia==1 ,1,0),
Diabetes_Hypertension_Asthma_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 ,1,0),
Diabetes_Obesity_Asthma_Anemia= ifelse(dhs_nomiss$ex_diab_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia= ifelse(dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia_Diabetes= ifelse(dhs_nomiss$ex_htn_broad_ind==1& dhs_nomiss$obese==1& dhs_nomiss$asthma==1& dhs_nomiss$anemia==1 & dhs_nomiss$ex_diab_broad_ind==1 ,1,0))



###create PSU ID in dhs_nomiss_diabetic_twotreatedcomorb :
dhs_nomiss_interact <- dhs_nomiss %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_interact$psuid <- as.factor(dhs_nomiss_interact$psuid)

###make noNAinpsu

dhs_nomiss_interact_noNAinpsu <- filter(dhs_nomiss_interact, is.na(psu)==F)

summary(dhs_nomiss_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_interact_noNAinpsu$psu)==T)



 svy_all <- dhs_nomiss_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension,
   Diabetes_Obesity,
                     Hypertension_Obesity,
                     Asthma_Obesity,
                     Anemia_Obesity,
  Diabetes_Hypertension_Obesity,
Diabetes_Hypertension_Asthma,
Diabetes_Hypertension_Anemia,
Diabetes_Obesity_Asthma,
Diabetes_Obesity_Anemia,
Diabetes_Asthma_Anemia,
Hypertension_Obesity_Asthma,
Hypertension_Obesity_Anemia,
Hypertension_Asthma_Anemia,
Obesity_Asthma_Anemia))

  
  prevtot2morb <- svy_all %>%
    summarize(Hypertension_Obesity_prop = survey_mean(Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100,
              Asthma_Obesity_prop = survey_mean(Asthma_Obesity, proportion=TRUE, vartype = "ci")*100,
              Anemia_Obesity_prop = survey_mean(Anemia_Obesity, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Obesity_prop = survey_mean(Diabetes_Obesity, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100 ,
               anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_asthma_prop = survey_mean(diabetes_asthma, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             Diabetes_Hypertension_Obesity_prop = survey_mean(Diabetes_Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Asthma_prop = survey_mean(Diabetes_Hypertension_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Anemia_prop = survey_mean(Diabetes_Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Asthma_prop = survey_mean(Diabetes_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Anemia_prop = survey_mean(Diabetes_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Asthma_Anemia_prop = survey_mean(Diabetes_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Asthma_prop = survey_mean(Hypertension_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Anemia_prop = survey_mean(Hypertension_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Asthma_Anemia_prop = survey_mean(Hypertension_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Obesity_Asthma_Anemia_prop = survey_mean(Obesity_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100)
       
       

write.csv(prevtot2morb, "prevalence 2way interactions DHS .csv")

prevalence.2way.interactions.DHS. <- read.csv("~/Documents/Public Health Files/Public Health/public health/paper multimorbidity/prevalence 2way interactions DHS .csv")
inter <- prevtot2morb

#prevalence.2way.interactions.DHS.anemiahyp.only. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades #with morbidities/prevalence 2way interactions DHS anemiahyp only .csv")

inter$anemia_diabetes_prop <- as.numeric(inter$anemia_diabetes_prop)
inter$anemia_asthma_prop <- as.numeric(inter$anemia_asthma_prop)
inter$anemia_hypertension_prop <- as.numeric(inter$anemia_hypertension_prop)
inter$diabetes_asthma_prop <- as.numeric(inter$diabetes_asthma_prop)
inter$diabetes_hypertension_prop <- as.numeric(inter$diabetes_hypertension_prop)
inter$asthma_hypertension_prop <- as.numeric(inter$asthma_hypertension_prop)
inter$Diabetes_Obesity_prop <- as.numeric(inter$Diabetes_Obesity_prop)
inter$Hypertension_Obesity_prop <- as.numeric(inter$Hypertension_Obesity_prop)
inter$Asthma_Obesity_prop <- as.numeric(inter$Asthma_Obesity_prop)
inter$Anemia_Obesity_prop <- as.numeric(inter$Anemia_Obesity_prop)
inter$Diabetes_Hypertension_Obesity_prop <- as.numeric(inter$Diabetes_Hypertension_Obesity_prop)
inter$Diabetes_Hypertension_Asthma_prop <- as.numeric(inter$Diabetes_Hypertension_Asthma_prop)
inter$Diabetes_Hypertension_Anemia_prop <- as.numeric(inter$Diabetes_Hypertension_Anemia_prop)
inter$Diabetes_Obesity_Asthma_prop <- as.numeric(inter$Diabetes_Obesity_Asthma_prop)
inter$Diabetes_Obesity_Anemia_prop <- as.numeric(inter$Diabetes_Obesity_Anemia_prop)
inter$Diabetes_Asthma_Anemia_prop <- as.numeric(inter$Diabetes_Asthma_Anemia_prop)
inter$Hypertension_Obesity_Asthma_prop <- as.numeric(inter$Hypertension_Obesity_Asthma_prop)
inter$Hypertension_Obesity_Anemia_prop <- as.numeric(inter$Hypertension_Obesity_Anemia_prop)
inter$Hypertension_Asthma_Anemia_prop <- as.numeric(inter$Hypertension_Asthma_Anemia_prop)
inter$Obesity_Asthma_Anemia_prop <- as.numeric(inter$Obesity_Asthma_Anemia_prop)


#inter2 <- prevalence.2way.interactions.DHS.anemiahyp.only.

mycols <- colors()[c(630, 630, 630, 630, 630, 556,630,630,556,630,630,630,556,556,556,556,556,556,556,556)] 

  Upset <- c('Anemia&Diabetes'= (inter$anemia_diabetes_prop)/100*712822,
  'Anemia&Asthma'= (inter$anemia_asthma_prop)/100*712822,
  'Anemia&Hypertension'= (inter$anemia_hypertension_prop)/100*712822,
  'Diabetes&Asthma'= (inter$diabetes_asthma_prop)/100*712822,
  'Diabetes&Hypertension'= (inter$diabetes_hypertension_prop)/100*712822,
  'Asthma&Hypertension'= (inter$asthma_hypertension_prop)/100*712822,
    'Diabetes&Obesity'= (inter$Diabetes_Obesity_prop)/100*712822,
    'Hypertension&Obesity'= (inter$Hypertension_Obesity_prop)/100*712822,
    'Asthma&Obesity'= (inter$Asthma_Obesity_prop)/100*712822,
    'Anemia&Obesity'= (inter$Anemia_Obesity_prop)/100*712822,
  'Diabetes&Hypertension&Obesity'= (inter$Diabetes_Hypertension_Obesity_prop)/100*712822,
    'Diabetes&Hypertension&Asthma'= (inter$Diabetes_Hypertension_Asthma_prop)/100*712822,
    'Diabetes&Hypertension&Anemia'= (inter$Diabetes_Hypertension_Anemia_prop)/100*712822,
    'Diabetes&Obesity&Asthma'= (inter$Diabetes_Obesity_Asthma_prop)/100*712822,
    'Diabetes&Obesity&Anemia'= (inter$Diabetes_Obesity_Anemia_prop)/100*712822,
    'Diabetes&Asthma&Anemia'= (inter$Diabetes_Asthma_Anemia_prop)/100*712822,
    'Hypertension&Obesity&Asthma'= (inter$Hypertension_Obesity_Asthma_prop)/100*712822,
    'Hypertension&Obesity&Anemia'= (inter$Hypertension_Obesity_Anemia_prop)/100*712822,
    'Hypertension&Asthma&Anemia'= (inter$Hypertension_Asthma_Anemia_prop)/100*712822,
    'Obesity&Asthma&Anemia'= (inter$Obesity_Asthma_Anemia_prop)/100*712822)
  
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=5, mainbar.y.label = "Number of Individuals", main.bar.color=mycols)     
  

```



```{r Upsetr for HIV made as all morbidity upsetr NEW NEW NEW}


dhs_nomiss_HIV <- filter(dhs_nomiss, hiv03==1)


dhs_nomiss_HIV <- mutate(dhs_nomiss_HIV,
                         Diabetes_HIV= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1,1,0),
                         Hypertension_HIV= ifelse(dhs_nomiss_HIV$ex_htn_broad_ind==1,1,0),
                         Obesity_HIV= ifelse(dhs_nomiss_HIV$obese==1,1,0),
                                 Asthma_HIV= ifelse(dhs_nomiss_HIV$asthma==1,1,0),
                                 Anemia_HIV= ifelse(dhs_nomiss_HIV$anemia==1,1,0),
                     Diabetes_Obesity= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$obese==1 ,1,0),
                     Hypertension_Obesity= ifelse(dhs_nomiss_HIV$ex_htn_broad_ind==1&  dhs_nomiss_HIV$obese==1 ,1,0),
                     Asthma_Obesity= ifelse(dhs_nomiss_HIV$asthma==1& dhs_nomiss_HIV$obese==1 ,1,0),
                     Anemia_Obesity= ifelse(dhs_nomiss_HIV$anemia==1&  dhs_nomiss_HIV$obese==1 ,1,0),
Diabetes_Hypertension_Obesity= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$obese==1 ,1,0),
Diabetes_Hypertension_Asthma= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$asthma==1 ,1,0),
Diabetes_Hypertension_Anemia= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$anemia==1 ,1,0),
Diabetes_Obesity_Asthma= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$asthma==1 ,1,0),
Diabetes_Obesity_Anemia= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$anemia==1 ,1,0),
Diabetes_Asthma_Anemia= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$anemia==1 ,1,0),
Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$asthma==1 ,1,0),
Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$anemia==1 ,1,0),
Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$asthma==1& dhs_nomiss_HIV$anemia==1 ,1,0),
Obesity_Asthma_Anemia= ifelse(dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$asthma==1& dhs_nomiss_HIV$anemia==1 ,1,0),
Diabetes_Hypertension_Obesity_Asthma= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$asthma==1 ,1,0),
Diabetes_Hypertension_Obesity_Anemia= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$anemia==1 ,1,0),
Diabetes_Hypertension_Asthma_Anemia= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$asthma==1& dhs_nomiss_HIV$anemia==1 ,1,0),
Diabetes_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$asthma==1& dhs_nomiss_HIV$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia= ifelse(dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$asthma==1& dhs_nomiss_HIV$anemia==1 ,1,0),
Hypertension_Obesity_Asthma_Anemia_Diabetes= ifelse(dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$asthma==1& dhs_nomiss_HIV$anemia==1 & dhs_nomiss_HIV$ex_diab_broad_ind==1 ,1,0))



###create PSU ID in dhs_nomiss_HIV_diabetic_twotreatedcomorb :
dhs_nomiss_HIV_interact <- dhs_nomiss_HIV %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_HIV_interact$psuid <- as.factor(dhs_nomiss_HIV_interact$psuid)

###make noNAinpsu

dhs_nomiss_HIV_interact_noNAinpsu <- filter(dhs_nomiss_HIV_interact, is.na(psu)==F)

summary(dhs_nomiss_HIV_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_HIV_interact_noNAinpsu$psu)==T)



 svy_all <- dhs_nomiss_HIV_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( Diabetes_HIV, Hypertension_HIV, Asthma_HIV, Anemia_HIV, Obesity_HIV,anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension,
   Diabetes_Obesity,
                     Hypertension_Obesity,
                     Asthma_Obesity,
                     Anemia_Obesity,
  Diabetes_Hypertension_Obesity,
Diabetes_Hypertension_Asthma,
Diabetes_Hypertension_Anemia,
Diabetes_Obesity_Asthma,
Diabetes_Obesity_Anemia,
Diabetes_Asthma_Anemia,
Hypertension_Obesity_Asthma,
Hypertension_Obesity_Anemia,
Hypertension_Asthma_Anemia,
Obesity_Asthma_Anemia))

  
  prevtot2morb <- svy_all %>%
    summarize(Hypertension_HIV_prop = survey_mean(Hypertension_HIV, proportion=TRUE, vartype = "ci")*100,
              Obesity_HIV_prop = survey_mean(Obesity_HIV, proportion=TRUE, vartype = "ci")*100,
              Diabetes_HIV_prop = survey_mean(Diabetes_HIV, proportion=TRUE, vartype = "ci")*100,
              Asthma_HIV_prop = survey_mean(Asthma_HIV, proportion=TRUE, vartype = "ci")*100,
              Anemia_HIV_prop = survey_mean(Anemia_HIV, proportion=TRUE, vartype = "ci")*100,
      Hypertension_Obesity_prop = survey_mean(Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100,
              Asthma_Obesity_prop = survey_mean(Asthma_Obesity, proportion=TRUE, vartype = "ci")*100,
              Anemia_Obesity_prop = survey_mean(Anemia_Obesity, proportion=TRUE, vartype = "ci")*100,
              Diabetes_Obesity_prop = survey_mean(Diabetes_Obesity, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100 ,
               anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_asthma_prop = survey_mean(diabetes_asthma, proportion=TRUE, vartype = "ci")*100 ,
              diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             Diabetes_Hypertension_Obesity_prop = survey_mean(Diabetes_Hypertension_Obesity, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Asthma_prop = survey_mean(Diabetes_Hypertension_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Hypertension_Anemia_prop = survey_mean(Diabetes_Hypertension_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Asthma_prop = survey_mean(Diabetes_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Obesity_Anemia_prop = survey_mean(Diabetes_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Diabetes_Asthma_Anemia_prop = survey_mean(Diabetes_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Asthma_prop = survey_mean(Hypertension_Obesity_Asthma, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Obesity_Anemia_prop = survey_mean(Hypertension_Obesity_Anemia, proportion=TRUE, vartype = "ci")*100 ,
        Hypertension_Asthma_Anemia_prop = survey_mean(Hypertension_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100 ,
       Obesity_Asthma_Anemia_prop = survey_mean(Obesity_Asthma_Anemia, proportion=TRUE, vartype = "ci")*100)
       
       

write.csv(prevtot2morb, "prevalence 2way interactions DHS HIV .csv")

prevalence.2way.interactions.DHS. <- read.csv("~/Documents/Public Health Files/Public Health/public health/paper multimorbidity/prevalence 2way interactions DHS .csv")
inter <- prevtot2morb

#prevalence.2way.interactions.DHS.anemiahyp.only. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades #with morbidities/prevalence 2way interactions DHS anemiahyp only .csv")

inter$anemia_diabetes_prop <- as.numeric(inter$anemia_diabetes_prop)
inter$Anemia_HIV_prop <- as.numeric(inter$Anemia_HIV_prop)
inter$Diabetes_HIV_prop <- as.numeric(inter$Diabetes_HIV_prop)
inter$Hypertension_HIV_prop <- as.numeric(inter$Hypertension_HIV_prop)
inter$Asthma_HIV_prop <- as.numeric(inter$Asthma_HIV_prop)
inter$Obesity_HIV_prop <- as.numeric(inter$Obesity_HIV_prop)
inter$anemia_asthma_prop <- as.numeric(inter$anemia_asthma_prop)
inter$anemia_hypertension_prop <- as.numeric(inter$anemia_hypertension_prop)
inter$diabetes_asthma_prop <- as.numeric(inter$diabetes_asthma_prop)
inter$diabetes_hypertension_prop <- as.numeric(inter$diabetes_hypertension_prop)
inter$asthma_hypertension_prop <- as.numeric(inter$asthma_hypertension_prop)
inter$Diabetes_Obesity_prop <- as.numeric(inter$Diabetes_Obesity_prop)
inter$Hypertension_Obesity_prop <- as.numeric(inter$Hypertension_Obesity_prop)
inter$Asthma_Obesity_prop <- as.numeric(inter$Asthma_Obesity_prop)
inter$Anemia_Obesity_prop <- as.numeric(inter$Anemia_Obesity_prop)
inter$Diabetes_Hypertension_Obesity_prop <- as.numeric(inter$Diabetes_Hypertension_Obesity_prop)
inter$Diabetes_Hypertension_Asthma_prop <- as.numeric(inter$Diabetes_Hypertension_Asthma_prop)
inter$Diabetes_Hypertension_Anemia_prop <- as.numeric(inter$Diabetes_Hypertension_Anemia_prop)
inter$Diabetes_Obesity_Asthma_prop <- as.numeric(inter$Diabetes_Obesity_Asthma_prop)
inter$Diabetes_Obesity_Anemia_prop <- as.numeric(inter$Diabetes_Obesity_Anemia_prop)
inter$Diabetes_Asthma_Anemia_prop <- as.numeric(inter$Diabetes_Asthma_Anemia_prop)
inter$Hypertension_Obesity_Asthma_prop <- as.numeric(inter$Hypertension_Obesity_Asthma_prop)
inter$Hypertension_Obesity_Anemia_prop <- as.numeric(inter$Hypertension_Obesity_Anemia_prop)
inter$Hypertension_Asthma_Anemia_prop <- as.numeric(inter$Hypertension_Asthma_Anemia_prop)
inter$Obesity_Asthma_Anemia_prop <- as.numeric(inter$Obesity_Asthma_Anemia_prop)


#inter2 <- prevalence.2way.interactions.DHS.anemiahyp.only.

mycols <- colors()[c(630, 630, 630, 556, 630, 556,556,630,556,556,556,556,556)] 

  Upset <- c('Anemia&HIV'= (inter$Anemia_HIV_prop)/100*469,
             'Obesity&HIV'= (inter$Obesity_HIV_prop)/100*469,
             'Hypertension&HIV'= (inter$Hypertension_HIV_prop)/100*469,
             'Diabetes&HIV'= (inter$Diabetes_HIV_prop)/100*469,
             'Asthma&HIV'= (inter$Asthma_HIV_prop)/100*469,
    'Anemia&Diabetes&HIV'= (inter$anemia_diabetes_prop)/100*469,
  'Anemia&Asthma&HIV'= (inter$anemia_asthma_prop)/100*469,
  'Anemia&Hypertension&HIV'= (inter$anemia_hypertension_prop)/100*469,
  'Diabetes&Asthma&HIV'= (inter$diabetes_asthma_prop)/100*469,
  'Diabetes&Hypertension&HIV'= (inter$diabetes_hypertension_prop)/100*469,
  'Asthma&Hypertension&HIV'= (inter$asthma_hypertension_prop)/100*469,
    'Diabetes&Obesity&HIV'= (inter$Diabetes_Obesity_prop)/100*469,
    'Hypertension&Obesity&HIV'= (inter$Hypertension_Obesity_prop)/100*469,
    'Asthma&Obesity&HIV'= (inter$Asthma_Obesity_prop)/100*469,
    'Anemia&Obesity&HIV'= (inter$Anemia_Obesity_prop)/100*469)
  
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=6, mainbar.y.label = "Number of Individuals", main.bar.color=mycols)   



  
#    'Diabetes&Hypertension&Obesity&HIV'= (inter$Diabetes_Hypertension_Obesity_prop)/100*469,
 #   'Diabetes&Hypertension&Asthma&HIV'= (inter$Diabetes_Hypertension_Asthma_prop)/100*469,
 #   'Diabetes&Hypertension&Anemia&HIV'= (inter$Diabetes_Hypertension_Anemia_prop)/100*469,
 #   'Diabetes&Obesity&Asthma&HIV'= (inter$Diabetes_Obesity_Asthma_prop)/100*469,
 #   'Diabetes&Obesity&Anemia&HIV'= (inter$Diabetes_Obesity_Anemia_prop)/100*469,
 #   'Diabetes&Asthma&Anemia&HIV'= (inter$Diabetes_Asthma_Anemia_prop)/100*469,
 #   'Hypertension&Obesity&Asthma&HIV'= (inter$Hypertension_Obesity_Asthma_prop)/100*469,
  #  'Hypertension&Obesity&Anemia&HIV'= (inter$Hypertension_Obesity_Anemia_prop)/100*469,
  #  'Hypertension&Asthma&Anemia&HIV'= (inter$Hypertension_Asthma_Anemia_prop)/100*469,
 #   'Obesity&Asthma&Anemia&HIV'= (inter$Obesity_Asthma_Anemia_prop)/100*469

```

```{r Upsetr DHS Morbidities RIGHT for HIV dataset}

dhs_nomiss_HIV <- filter(dhs_nomiss, hiv03==1)

###create mmissing categories


  dhs_nomiss_HIV <- dhs_nomiss_HIV %>% 
          mutate(diabetes_hiv = ifelse(ex_diab_broad_ind==1,1,0)) %>% 
    mutate(hypertension_hiv = ifelse(ex_htn_broad_ind==1,1,0)) %>% 
    mutate(anemia_hiv = ifelse(anemia==1,1,0)) %>% 
    mutate(asthma_hiv = ifelse(asthma==1,1,0)) %>% 
        mutate(obese_hiv = ifelse(obese==1,1,0)) %>% 
  mutate(anemia_diabetes_asthma = ifelse(anemia==1 &ex_diab_broad_ind==1 & asthma==1 ,1,0)) %>% 
 mutate(anemia_diabetes_hypertension = ifelse(anemia==1 & ex_diab_broad_ind==1 & ex_htn_broad_ind==1 ,1,0)) %>% 
     mutate(anemia_asthma_hypertension = ifelse(anemia==1 & asthma==1 & ex_htn_broad_ind==1 ,1,0)) %>%
       mutate(anemia_obese = ifelse(anemia==1 & obese==1 ,1,0)) %>%
      mutate(asthma_obese = ifelse(asthma==1 & obese==1 ,1,0)) %>%
      mutate(hypertension_obese = ifelse(hypertension_obese==1 & obese==1 ,1,0)) %>%
   mutate(diabetes_asthma_hypertension = ifelse(ex_diab_broad_ind==1 & asthma==1 & ex_htn_broad_ind==1 ,1,0)) 
   
  
  


#  'HIV&Diabetes&Asthma&Hypertension'= (inter$diabetes_asthma_hypertension_prop)/100*732515)

  
###create PSU ID in dhs_nomiss_diabetic_twotreatedcomorb :
dhs_nomiss_interact <- dhs_nomiss_HIV %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_interact$psuid <- as.factor(dhs_nomiss_interact$psuid)

###make noNAinpsu

dhs_nomiss_interact_noNAinpsu <- filter(dhs_nomiss_interact, is.na(psu)==F)

summary(dhs_nomiss_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_interact_noNAinpsu$psu)==T)


 svy_all <- dhs_nomiss_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( diabetes_hiv, hypertension_hiv, anemia_hiv, asthma_hiv, anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension,
   anemia_diabetes_asthma, 
 anemia_diabetes_hypertension, 
     anemia_asthma_hypertension,
   diabetes_asthma_hypertension))


  
  prevtot2morb <- svy_all %>%
    summarize(   diabetes_hiv_prop = survey_mean(diabetes_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    hypertension_hiv_prop = survey_mean(hypertension_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    anemia_hiv_prop = survey_mean(anemia_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    asthma_hiv_prop = survey_mean(asthma_hiv, proportion=TRUE, vartype = "ci")*100 ,
      anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100,
               anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100,
              diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100,
      diabetes_asthma_prop = survey_mean(diabetes_asthma, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_asthma_prop = survey_mean(anemia_diabetes_asthma, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_hypertension_prop = survey_mean(anemia_diabetes_hypertension, proportion=TRUE, vartype = "ci")*100,
      anemia_asthma_hypertension_prop = survey_mean(anemia_asthma_hypertension, proportion=TRUE, vartype = "ci")*100,
      diabetes_asthma_hypertension_prop = survey_mean(diabetes_asthma_hypertension, proportion=TRUE, vartype = "ci")*100)
     
  
write.csv(prevtot2morb, "prevalence 2way interactions DHS_HIV .csv")

prevalence.2way.interactions.DHS. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/prevalence 2way interactions DHS_HIV .csv")
inter <- prevalence.2way.interactions.DHS.


    heatcols <- hsv(1, 1, seq(1,0,length.out = 9))
  

  Upset <- c('HIV&Diabetes'= (inter$diabetes_hiv_prop)/100*469,
             'HIV&Hypertension'= (inter$hypertension_hiv_prop)/100*469,
             'HIV&Anemia'= (inter$anemia_hiv_prop)/100*469,
             'HIV&Asthma'= (inter$asthma_hiv_prop)/100*469,
             'HIV&Anemia&Asthma'= (inter$anemia_asthma_prop)/100*469,
 'HIV&Anemia&Hypertension'= (inter$anemia_hypertension_prop)/100*469,
 'HIV&Diabetes&Asthma'= (inter$diabetes_asthma_prop)/100*469,
  'HIV&Diabetes&Hypertension'= (inter$diabetes_hypertension_prop)/100*469,
  'HIV&Asthma&Hypertension'= (inter$asthma_hypertension_prop)/100*469,
 'HIV&Diabetes&Anemia'= (inter$anemia_diabetes_prop)/100*469,
  'HIV&Anemia&Diabetes&Asthma'= (inter$anemia_diabetes_asthma_prop)/100*469,
  'HIV&Anemia&Diabetes&Hypertension'= (inter$anemia_diabetes_hypertension_prop)/100*469,
  'HIV&Anemia&Asthma&Hypertension'= (inter$ anemia_asthma_hypertension_prop)/100*469,
  'HIV&Diabetes&Asthma&Hypertension'= (inter$diabetes_asthma_hypertension_prop)/100*469)
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=5, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
```





```{r Upsetr DHS CVD risc factor RIGHT for HIV dataset}

dhs_nomiss_HIV <- filter(dhs_nomiss, hiv03==1)


#####create correct morbiditiy categories

dhs_nomiss_HIV <- mutate(dhs_nomiss_HIV, 
               obese = ifelse(bmi>=27.5,1,0))

  dhs_nomiss_HIV <- dhs_nomiss_HIV %>% 
    mutate(uncontrolled_diabetes_hiv = ifelse(ex_diab_broad_ind==1,1,0)) %>% 
    mutate(uncontrolled_hypertension_hiv = ifelse(ex_htn_broad_ind==1,1,0)) %>%  
    mutate(smoking_hiv = ifelse(tobacco_smoked==1,1,0)) %>% 
    mutate(obese_hiv = ifelse(obese==1,1,0))
  
   dhs_nomiss_HIV <- dhs_nomiss_HIV  %>%
  mutate( 
diabetes_hypertension= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$ex_htn_broad_ind==1,1,0),
diabetes_smoke= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$tobacco_smoked==1,1,0),
diabetes_tobacco_smokedless= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$tobacco_smokeless==1,1,0),
diabetes_obese= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$obese==1,1,0),
diabetes_underweight= ifelse(dhs_nomiss_HIV$ex_diab_broad_ind==1& dhs_nomiss_HIV$sev_underweight==1,1,0),
hypertension_smoke= ifelse(dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$tobacco_smoked==1,1,0),
hypertension_tobacco_smokedless= ifelse(dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$tobacco_smokeless==1,1,0),
hypertension_obese= ifelse(dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$obese==1,1,0),
hypertension_underweight= ifelse(dhs_nomiss_HIV$ex_htn_broad_ind==1& dhs_nomiss_HIV$sev_underweight==1,1,0),
smoke_tobacco_smokedless= ifelse(dhs_nomiss_HIV$tobacco_smoked==1& dhs_nomiss_HIV$tobacco_smokeless==1,1,0),
smoke_obese = ifelse(dhs_nomiss_HIV$tobacco_smoked==1& dhs_nomiss_HIV$obese==1,1,0),
smoke_underweight= ifelse(dhs_nomiss_HIV$tobacco_smoked==1& dhs_nomiss_HIV$sev_underweight==1,1,0),
tobacco_smokedless_obese= ifelse(dhs_nomiss_HIV$tobacco_smokeless==1& dhs_nomiss_HIV$obese==1,1,0),
tobacco_smokedless_underweight= ifelse(dhs_nomiss_HIV$tobacco_smokeless==1& dhs_nomiss_HIV$sev_underweight==1,1,0),
obese_underweight= ifelse(dhs_nomiss_HIV$obese==1& dhs_nomiss_HIV$sev_underweight==1,1,0))

###create PSU ID in dhs_nomiss_diabetic_twotreatedcomorb :
dhs_nomiss_interact <- dhs_nomiss_HIV %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_interact$psuid <- as.factor(dhs_nomiss_interact$psuid)

###make noNAinpsu

dhs_nomiss_interact_noNAinpsu <- filter(dhs_nomiss_interact, is.na(psu)==F)

summary(dhs_nomiss_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_interact_noNAinpsu$psu)==T)


 svy_all <- dhs_nomiss_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( uncontrolled_diabetes_hiv, uncontrolled_hypertension_hiv, smoking_hiv, obese_hiv, diabetes_hypertension,diabetes_smoke, diabetes_obese, hypertension_smoke, hypertension_obese, smoke_obese))

  
  prevtot2morb <- svy_all %>%
    summarize(   uncontrolled_diabetes_hiv_prop = survey_mean(uncontrolled_diabetes_hiv, proportion=TRUE, vartype = "ci")*100 ,
                   uncontrolled_hypertension_hiv_prop = survey_mean(uncontrolled_hypertension_hiv, proportion=TRUE, vartype = "ci")*100 ,
                   smoking_hiv_prop = survey_mean(smoking_hiv, proportion=TRUE, vartype = "ci")*100 ,
                   obese_hiv_prop = survey_mean(obese_hiv, proportion=TRUE, vartype = "ci")*100 ,
                   diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
                   diabetes_smoke_prop = survey_mean(diabetes_smoke, proportion=TRUE, vartype = "ci")*100 ,
                   diabetes_obese_prop = survey_mean(diabetes_obese, proportion=TRUE, vartype = "ci")*100 ,
                   hypertension_smoke_prop = survey_mean(hypertension_smoke, proportion=TRUE, vartype = "ci")*100 ,
                   hypertension_obese_prop = survey_mean(hypertension_obese, proportion=TRUE, vartype = "ci")*100 ,
                   smoke_obese_prop = survey_mean(smoke_obese, proportion=TRUE, vartype = "ci")*100)

write.csv(prevtot2morb, "prevalence 2way interactions DHS_HIV .csv")

prevalence.2way.interactions.DHS. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/prevalence 2way interactions DHS_HIV .csv")
inter <- prevalence.2way.interactions.DHS.


    heatcols <- hsv(1, 1, seq(1,0,length.out = 7))
  

  Upset <- c('HIV&raised Glucose'= (inter$uncontrolled_diabetes_hiv_prop)/100*469,
             'HIV&raised BP'= (inter$uncontrolled_hypertension_hiv_prop)/100*469,
             'HIV&Smoking'= (inter$smoking_hiv_prop)/100*469,
             'HIV&Obese'= (inter$obese_hiv_prop)/100*469,
           'HIV&raised Glucose&raised BP' = (inter$diabetes_hypertension_prop)/100*469,
             'HIV&raised Glucose&Smoking' = (inter$diabetes_smoke_prop)/100*469,
             'HIV&raised Glucose&Obese'= (inter$diabetes_obese_prop)/100*469,
             'HIV&raised BP&Smoking'= (inter$hypertension_smoke_prop)/100*469,
              'HIV&raised BP&Obese'= (inter$hypertension_obese_prop)/100*469,
             'HIV&Smoking&Obese'= (inter$smoke_obese_prop)/100*469)

  
  upset(fromExpression(Upset), order.by = "freq",nsets=5, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
```









```{r Upsetr DHS ALL risc factor HIV dataset}

dhs_nomiss_HIV <- filter(dhs_nomiss, hiv03==1)


#####create correct morbiditiy categories
dhs_nomiss_HIV <- mutate(dhs_nomiss_HIV, 
               sev_underweight = ifelse(bmi<16,1,0))

dhs_nomiss_HIV <- mutate(dhs_nomiss_HIV, 
               obese = ifelse(bmi>=27.5,1,0))

  dhs_nomiss_HIV <- dhs_nomiss_HIV %>% 
          mutate(diabetes_hiv = ifelse(ex_diab_broad_ind==1,1,0)) %>% 
    mutate(hypertension_hiv = ifelse(ex_htn_broad_ind==1,1,0)) %>% 
    mutate(anemia_hiv = ifelse(anemia==1,1,0)) %>% 
    mutate(asthma_hiv = ifelse(asthma==1,1,0)) %>% 
    mutate(uncontrolled_diabetes_hiv = ifelse(ex_diab_broad_ind==1,1,0)) %>% 
    mutate(uncontrolled_hypertension_hiv = ifelse(ex_htn_broad_ind==1,1,0)) %>%  
    mutate(smoking_hiv = ifelse(tobacco_smoked==1,1,0)) %>% 
    mutate(smokeless_tobacco_hiv = ifelse(tobacco_smokeless==1,1,0)) %>% 
    mutate(underweight_hiv = ifelse(sev_underweight==1,1,0)) %>% 
    mutate(obese_hiv = ifelse(obese==1,1,0)) 

###create PSU ID in dhs_nomiss_diabetic_twotreatedcomorb :
dhs_nomiss_interact <- dhs_nomiss_HIV %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_interact$psuid <- as.factor(dhs_nomiss_interact$psuid)

###make noNAinpsu

dhs_nomiss_interact_noNAinpsu <- filter(dhs_nomiss_interact, is.na(psu)==F)

summary(dhs_nomiss_interact_noNAinpsu$psu)

sum(is.na(dhs_nomiss_interact_noNAinpsu$psu)==T)



 svy_all <- dhs_nomiss_interact_noNAinpsu %>% 
    as_survey_design(stratum = stratumid,
                     ids = c(psuid,hh_id),
                     weights = sworld_weight_india,
                     variables = c( diabetes_hiv, hypertension_hiv, anemia_hiv, asthma_hiv, uncontrolled_diabetes_hiv, uncontrolled_hypertension_hiv, smoking_hiv, smokeless_tobacco_hiv, underweight_hiv, obese_hiv, anemia_diabetes,
  anemia_asthma,
  anemia_hypertension,
  diabetes_asthma,
  diabetes_hypertension,
  asthma_hypertension))

  
  prevtot2morb <- svy_all %>%
    summarize(   diabetes_hiv_prop = survey_mean(diabetes_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    hypertension_hiv_prop = survey_mean(hypertension_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    anemia_hiv_prop = survey_mean(anemia_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    asthma_hiv_prop = survey_mean(asthma_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    uncontrolled_diabetes_hiv_prop = survey_mean(uncontrolled_diabetes_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    uncontrolled_hypertension_hiv_prop = survey_mean(uncontrolled_hypertension_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    smoking_hiv_prop = survey_mean(smoking_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    smokeless_tobacco_hiv_prop = survey_mean(smokeless_tobacco_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    underweight_hiv_prop = survey_mean(underweight_hiv, proportion=TRUE, vartype = "ci")*100 ,
                    obese_hiv_prop = survey_mean(obese_hiv, proportion=TRUE, vartype = "ci")*100,
      anemia_diabetes_prop = survey_mean(anemia_diabetes, proportion=TRUE, vartype = "ci")*100,
              anemia_asthma_prop = survey_mean(anemia_asthma, proportion=TRUE, vartype = "ci")*100,
               anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100,
              diabetes_hypertension_prop = survey_mean(diabetes_hypertension, proportion=TRUE, vartype = "ci")*100 ,
             asthma_hypertension_prop = survey_mean(asthma_hypertension, proportion=TRUE, vartype = "ci")*100)
     
   prevtot2anemiahyponly <- svy_all %>%
    summarize(anemia_hypertension_prop = survey_mean(anemia_hypertension, proportion=TRUE, vartype = "ci")*100) 

write.csv(prevtot2morb, "prevalence 2way interactions DHS_HIV_ALL .csv")
write.csv(prevtot2anemiahyponly, "prevalence 2way interactions DHS_HIV anemiahyp only .csv")

prevalence.2way.interactions.DHS. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/prevalence 2way interactions DHS_HIV_ALL .csv")
inter <- prevalence.2way.interactions.DHS.

prevalence.2way.interactions.DHS.anemiahyp.only. <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/prevalence 2way interactions DHS_HIV anemiahyp only .csv")


inter2 <- prevalence.2way.interactions.DHS.anemiahyp.only.

    heatcols <- hsv(1, 1, seq(1,0,length.out = 10))
  

  Upset <- c('HIV&Diabetes'= (inter$diabetes_hiv_prop)/100*469,
             'HIV&Hypertension'= (inter$hypertension_hiv_prop)/100*469,
             'HIV&Anemia'= (inter$anemia_hiv_prop)/100*469,
             'HIV&Asthma'= (inter$asthma_hiv_prop)/100*469,
             'HIV&raised glucose'= (inter$uncontrolled_diabetes_hiv_prop)/100*469,
             'HIV&raised BP'= (inter$uncontrolled_hypertension_hiv_prop)/100*469,
             'HIV&Smoking'= (inter$smoking_hiv_prop)/100*469,
              'HIV&Smokeless tobacco'= (inter$smokeless_tobacco_hiv_prop)/100*469,
              'HIV&severe Underweight'= (inter$underweight_hiv_prop)/100*469,
              'HIV&Obese'= (inter$obese_hiv_prop)/100*469)
  
##  'HIV&Anemia&Asthma'= (inter$anemia_asthma_prop)/100*732515,
 ## 'HIV&Anemia&Hypertension'= (inter2$anemia_hypertension_prop)/100*732515,
 ## 'HIV&Diabetes&Asthma'= (inter$diabetes_asthma_prop)/100*732515,
 ## 'HIV&Diabetes&Hypertension'= (inter$diabetes_hypertension_prop)/100*732515,
 ## 'HIV&Asthma&Hypertension'= (inter$asthma_hypertension_prop)/100*732515,
 # 'HIV&Anemia&Diabetes&Asthma'= (inter$anemia_diabetes_asthma_prop)/100*732515,
 # 'HIV&Anemia&Diabetes&Hypertension'= (inter$anemia_diabetes_hypertension_prop)/100*732515,
 # 'HIV&Anemia&Asthma&Hypertension'= (inter$ anemia_asthma_hypertension_prop)/100*732515,
 # 'HIV&Diabetes&Asthma&Hypertension'= (inter$diabetes_asthma_hypertension_prop)/100*732515)
  
  
  upset(fromExpression(Upset), order.by = "freq",nsets=11, mainbar.y.label = "Number of Individuals", main.bar.color=heatcols)     
  
```



```{r multivariable regressions with district random effects}

remove.packages(mediation)
remove.packages(miceadds)
 install.packages("lme4")
library(lme4)
library(broom)
 
 detach("package:mediation", unload=TRUE)
detach("package:miceadds", unload=TRUE)
 
 
 
 
dhs_nomiss$wealth_quintile_rurb <- as.numeric(dhs_nomiss$wealth_quintile_rurb)

#### make district wealth quintiles for regressions
        dhs_nomiss <- dhs_nomiss %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        dhs_nomiss <- dhs_nomiss %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))

   dhs_nomiss <- dhs_nomiss %>% 
          mutate(educat2class = ifelse(educatnames=="Primary school",1,
                                       ifelse(educatnames=="Middle school",1,
                                              ifelse(educatnames=="Secondary school",1,
                                                       ifelse(educatnames=="> Secondary school",1,0)))))

   dhs_nomiss$educat2classfactor <- as.factor(dhs_nomiss$educat2class)
   
    dhs_nomiss <- dhs_nomiss %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianeducat = median(educat2class, na.rm=TRUE)) %>% 
          ungroup()

        dhs_nomiss <- dhs_nomiss %>% 
          mutate(district_medianeducat = as.factor(ntile(medianeducat, 5)))
#####regression

###make urban rural dataset

dhs_nomiss_urban <- filter(dhs_nomiss, urban==1)
dhs_nomiss_rural <- filter(dhs_nomiss, urban==0)

#dhs_nomiss_urban_regress<-filter(dhs_nomiss_urban, is.na(dhs_nomiss_urban$multi_morbid_dbl)==F & is.na(dhs_nomiss_urban$age_grp)==F & is.na(dhs_nomiss_urban$wealth_quintile_rurb_lab)==F & is.na(dhs_nomiss_urban$educatnames)==F& is.na(dhs_nomiss_urban$married)==F& is.na(dhs_nomiss_urban$sex)==F& is.na(dhs_nomiss_urban$district_medianwealth)==F& is.na(dhs_nomiss_urban$d_id)==F)

#dhs_nomiss_rural_regress<-filter(dhs_nomiss_rural, is.na(dhs_nomiss_rural$multi_morbid_dbl)==F & is.na(dhs_nomiss_rural$age_grp)==F & is.na(dhs_nomiss_rural$wealth_quintile_rurb_lab)==F & is.na(dhs_nomiss_rural$educatnames)==F& is.na(dhs_nomiss_rural$married)==F& is.na(dhs_nomiss_rural$sex)==F& is.na(dhs_nomiss_rural$district_medianwealth)==F& is.na(dhs_nomiss_rural$d_id)==F)



multimorbidity_urban <- glmer(formula = multi_morbid_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + tobacco_smoked + tobacco_smokeless  + district_medianwealth + (1|d_id), data=dhs_nomiss_urban,family=binomial(link="logit"))


#results_urban <-tidy(multimorbidity_urban, exponentiate = TRUE, conf.int = TRUE, conf.level = 0.95)

#write.csv(results_urban, "RR multimorbidity glmer urban.csv")

multimorbidity_urban_poisson <- glmer(formula = multi_morbid_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + tobacco_smoked + tobacco_smokeless  + district_medianwealth + (1|d_id), data=dhs_nomiss_urban,family=poisson(link="log"))


results_urban <-tidy(multimorbidity_urban_poisson, exponentiate = TRUE, conf.int = TRUE, conf.level = 0.95)

write.csv(results_urban, "RR multimorbidity glmer urban poisson.csv")


tidy(multimorbidity_urban_poisson)
confint_tidy(multimorbidity_urban_poisson, conf.level = 0.95, func = stats::confint)

aware <- exp(cbind(RR = coef(multimorbidity_urban), confint.default(multimorbidity_urban)))
write.csv(aware, "RR multimorbidity poisson urban.csv")

results_aware <-summary$coefficients(multimorbidity_urban)
write.csv(results_aware, "p Value multimorbidity poisson urban.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR multimorbidity poisson urban.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value multimorbidity poisson urban.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 multimorbidity"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 21)
joint <- InsertRow(joint, NewRow = noref, RowNum = 21)
joint <- InsertRow(joint, NewRow = age, RowNum = 16)
joint <- InsertRow(joint, NewRow = noref, RowNum = 16)
joint <- InsertRow(joint, NewRow = age, RowNum = 9)
joint <- InsertRow(joint, NewRow = noref, RowNum = 9)
joint <- InsertRow(joint, NewRow = age, RowNum = 5)
joint <- InsertRow(joint, NewRow = noref, RowNum = 5)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "multimorbidity poisson urban.csv")
 

 
##rural##

## multimorbidity_rural

#multimorbidity_rural <- glmer(formula = multi_morbid_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + tobacco_smoked + tobacco_smokeless  + district_medianwealth + (1|d_id), data=dhs_nomiss_rural,family=binomial(link="logit"))

#multimorbidity_rural_poisson <- glmer(formula = multi_morbid_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex  + district_medianwealth+ educat2class + (1|d_id), data=dhs_nomiss_rural,family=poisson(link="log"))

#results_rural <-tidy(multimorbidity_rural, quick = FALSE, conf.int = TRUE,
#conf.level = 0.95)

#write.csv(results_rural, "RR multimorbidity glmer rural.csv")

multimorbidity_rural_poisson <- glmer(formula = multi_morbid_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + tobacco_smoked + tobacco_smokeless  + district_medianwealth + (1|d_id), data=dhs_nomiss_rural,family=poisson(link="log"))


results_rural <-tidy(multimorbidity_rural_poisson, exponentiate = TRUE, conf.int = TRUE, conf.level = 0.95)

write.csv(results_rural, "RR multimorbidity glmer rural poisson.csv")

x<-round(exp(cbind(OR=coef(multimorbidity_rural_poisson),confint(multimorbidity_rural_poisson))),3)


aware <- exp(cbind(RR = coef(multimorbidity_rural_poisson), confint(multimorbidity_rural_poisson)))
write.csv(aware, "RR multimorbidity_rural poisson rural.csv")

results_aware <-summary(multimorbidity_rural-poisson)
write.csv(results_aware, "p Value multimorbidity_rural poisson rural.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR multimorbidity_rural poisson rural.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value multimorbidity_rural poisson rural.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 multimorbidity_rural"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 21)
joint <- InsertRow(joint, NewRow = noref, RowNum = 21)
joint <- InsertRow(joint, NewRow = age, RowNum = 16)
joint <- InsertRow(joint, NewRow = noref, RowNum = 16)
joint <- InsertRow(joint, NewRow = age, RowNum = 9)
joint <- InsertRow(joint, NewRow = noref, RowNum = 9)
joint <- InsertRow(joint, NewRow = age, RowNum = 5)
joint <- InsertRow(joint, NewRow = noref, RowNum = 5)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "multimorbidity_rural poisson rural.csv")
 
 
 


```

```{r multivariable regressions morbidities!!!!}

dhs_nomiss$wealth_quintile_rurb <- as.numeric(dhs_nomiss$wealth_quintile_rurb)

#### make district wealth quintiles for regressions
        dhs_nomiss <- dhs_nomiss %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        dhs_nomiss <- dhs_nomiss %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))



#####regression

###make urban rural dataset

dhs_nomiss_urban <- filter(dhs_nomiss, urban==1)
dhs_nomiss_rural <- filter(dhs_nomiss, urban==0)



##URBAN##

## multimorbidity

multimorbidity <- glm.cluster(formula = multi_morbid_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + zone_new + district_medianwealth,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(multimorbidity), confint(multimorbidity)))
write.csv(aware, "RR multimorbidity poisson urban.csv")

results_aware <-summary(multimorbidity)
write.csv(results_aware, "p Value multimorbidity poisson urban.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR multimorbidity poisson urban.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value multimorbidity poisson urban.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 multimorbidity"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 21)
joint <- InsertRow(joint, NewRow = noref, RowNum = 21)
joint <- InsertRow(joint, NewRow = age, RowNum = 16)
joint <- InsertRow(joint, NewRow = noref, RowNum = 16)
joint <- InsertRow(joint, NewRow = age, RowNum = 9)
joint <- InsertRow(joint, NewRow = noref, RowNum = 9)
joint <- InsertRow(joint, NewRow = age, RowNum = 5)
joint <- InsertRow(joint, NewRow = noref, RowNum = 5)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "multimorbidity poisson urban.csv")
 

 
##rural##

## multimorbidity

multimorbidity <- glm.cluster(formula = multi_morbid_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married + sex + zone_new + district_medianwealth,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(multimorbidity), confint(multimorbidity)))
write.csv(aware, "RR multimorbidity poisson rural.csv")

results_aware <-summary(multimorbidity)
write.csv(results_aware, "p Value multimorbidity poisson rural.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR multimorbidity poisson rural.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value multimorbidity poisson rural.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 multimorbidity"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 21)
joint <- InsertRow(joint, NewRow = noref, RowNum = 21)
joint <- InsertRow(joint, NewRow = age, RowNum = 16)
joint <- InsertRow(joint, NewRow = noref, RowNum = 16)
joint <- InsertRow(joint, NewRow = age, RowNum = 9)
joint <- InsertRow(joint, NewRow = noref, RowNum = 9)
joint <- InsertRow(joint, NewRow = age, RowNum = 5)
joint <- InsertRow(joint, NewRow = noref, RowNum = 5)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "multimorbidity poisson rural.csv")
 
 
 
 
```




```{r univariable regressions multimorbidity}

library(miceadds)

dhs_nomiss$wealth_quintile_rurb <- as.numeric(dhs_nomiss$wealth_quintile_rurb)

#### make district wealth quintiles for regressions
        dhs_nomiss <- dhs_nomiss %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        dhs_nomiss <- dhs_nomiss %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))




################## zone_news as per: https://en.wikipedia.org/wiki/Administrative_divisions_of_India
dhs_nomiss <- mutate(dhs_nomiss, 
                  # Nothern
                  zone_new = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir", "North",
                                          # Northeastern
                                          ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                 # Central
                                                 ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                        # Eastern
                                                        ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                               # Western
                                                               ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                      # Southern
                                                                      ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))


#####regression

###make urban rural dataset

dhs_nomiss_urban <- filter(dhs_nomiss, urban==1)
dhs_nomiss_rural <- filter(dhs_nomiss, urban==0)



###univariable regressions multimorbidity URBAN    ##

tobacco_smoked <- glm.cluster(formula = multi_morbid_dbl ~ tobacco_smoked ,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results multimorbidity univariable reg urban.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value multimorbidity univariable reg urban.csv")

tobacco_smokeless <- glm.cluster(formula = multi_morbid_dbl ~ tobacco_smokeless ,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
tobacco_smokelesssheet <- exp(cbind(RR = coef(tobacco_smokeless), confint(tobacco_smokeless)))
write.csv(tobacco_smokelesssheet, "tobacco_smokeless RR regression results multimorbidity univariable reg urban.csv")

tobacco_smokeless_reg <-summary(tobacco_smokeless)
write.csv(tobacco_smokeless_reg, "tobacco_smokeless p-Value multimorbidity univariable reg urban.csv")

age <- glm.cluster(formula = multi_morbid_dbl ~ age_grp ,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results multimorbidity univariable reg urban.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value multimorbidity univariable reg urban.csv")

wealth <- glm.cluster(formula = multi_morbid_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results multimorbidity  univariable reg urban.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value multimorbidity univariable reg urban.csv")

educat <- glm.cluster(formula = multi_morbid_dbl ~  educatnames ,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results multimorbidity univariable reg urban.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value multimorbidity univariable reg urban.csv")

married <- glm.cluster(formula = multi_morbid_dbl ~  married ,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results multimorbidity univariable reg urban.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value multimorbidity univariable reg urban.csv")

sex <- glm.cluster(formula = multi_morbid_dbl ~  sex ,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results multimorbidity univariable reg urban.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value multimorbidity univariable reg urban.csv")

zone_new <- glm.cluster(formula = multi_morbid_dbl ~ zone_new,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results multimorbidity univariable reg urban.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value multimorbidity univariable reg urban.csv")


districtwealth <- glm.cluster(formula = multi_morbid_dbl ~ district_medianwealth ,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results multimorbidity univariable reg urban.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value multimorbidity univariable reg urban.csv")



###univariable regressions multimorbidity rural    ##

tobacco_smoked <- glm.cluster(formula = multi_morbid_dbl ~ tobacco_smoked ,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results multimorbidity univariable reg rural.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value multimorbidity univariable reg rural.csv")

tobacco_smokeless <- glm.cluster(formula = multi_morbid_dbl ~ tobacco_smokeless ,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
tobacco_smokelesssheet <- exp(cbind(RR = coef(tobacco_smokeless), confint(tobacco_smokeless)))
write.csv(tobacco_smokelesssheet, "tobacco_smokeless RR regression results multimorbidity univariable reg rural.csv")

tobacco_smokeless_reg <-summary(tobacco_smokeless)
write.csv(tobacco_smokeless_reg, "tobacco_smokeless p-Value multimorbidity univariable reg rural.csv")

age <- glm.cluster(formula = multi_morbid_dbl ~ age_grp ,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results multimorbidity univariable reg rural.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value multimorbidity univariable reg rural.csv")

wealth <- glm.cluster(formula = multi_morbid_dbl ~  wealth_quintile_rurb_lab ,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results multimorbidity  univariable reg rural.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value multimorbidity univariable reg rural.csv")

educat <- glm.cluster(formula = multi_morbid_dbl ~  educatnames ,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results multimorbidity univariable reg rural.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value multimorbidity univariable reg rural.csv")

married <- glm.cluster(formula = multi_morbid_dbl ~  married ,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results multimorbidity univariable reg rural.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value multimorbidity univariable reg rural.csv")

sex <- glm.cluster(formula = multi_morbid_dbl ~  sex ,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results multimorbidity univariable reg rural.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value multimorbidity univariable reg rural.csv")

zone_new <- glm.cluster(formula = multi_morbid_dbl ~ zone_new,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
zone_newsheet <- exp(cbind(RR = coef(zone_new), confint(zone_new)))
write.csv(zone_newsheet, "zone_new RR regression results multimorbidity univariable reg rural.csv")

zone_new_reg <-summary(zone_new)
write.csv(zone_new_reg, "zone_new p-Value multimorbidity univariable reg rural.csv")


districtwealth <- glm.cluster(formula = multi_morbid_dbl ~ district_medianwealth ,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
districtwealthsheet <- exp(cbind(RR = coef(districtwealth), confint(districtwealth)))
write.csv(districtwealthsheet, "districtwealth RR regression results multimorbidity univariable reg rural.csv")

districtwealth_reg <-summary(districtwealth)
write.csv(districtwealth_reg, "districtwealth p-Value multimorbidity univariable reg rural.csv")







```





```{r multivariable regressions morbidities!!!! with district-level fixed effects and robust standard errors}

#remove.packages(mediation)
#remove.packages(miceadds)
# install.packages("lme4")
#library(lme4)
#library(broom)
 
# detach("package:mediation", unload=TRUE)
#detach("package:miceadds", unload=TRUE)
 
 


dhs_nomiss$wealth_quintile_rurb <- as.numeric(dhs_nomiss$wealth_quintile_rurb)

#### make district wealth quintiles for regressions
        dhs_nomiss <- dhs_nomiss %>% 
          group_by(ex_state_ind) %>% 
          mutate(medianwealth = median(wealth_quintile_rurb, na.rm=TRUE)) %>% 
          ungroup()

        dhs_nomiss <- dhs_nomiss %>% 
          mutate(district_medianwealth = as.factor(ntile(medianwealth, 5)))




#####regression

###make urban rural dataset

dhs_nomiss_urban <- filter(dhs_nomiss, urban==1)
dhs_nomiss_rural <- filter(dhs_nomiss, urban==0)



##URBAN##

## multimorbidity

multimorbidity <- glm.cluster(formula = multi_morbid_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married  + sex + tobacco_smoked + tobacco_smokeless + d_id ,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(multimorbidity), confint(multimorbidity)))
write.csv(aware, "RR multimorbidity poisson urban district fixed effects1.csv")


results_aware <-summary(multimorbidity)
write.csv(results_aware, "p Value multimorbidity poisson urban district fixed effects1.csv")

coeftest(lmfit, vcov = vcovHC(lmfit))



##CLustered standard errors

cluster.im.glm(multimorbidity,dhs_nomiss_urban , cluster, ci.level = 0.95, report = TRUE,
drop = FALSE, truncate = FALSE, return.vcv = FALSE)

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR multimorbidity poisson urban district fixed effects1.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value multimorbidity poisson urban district fixed effects1.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 multimorbidity"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 21)
joint <- InsertRow(joint, NewRow = noref, RowNum = 21)
joint <- InsertRow(joint, NewRow = age, RowNum = 16)
joint <- InsertRow(joint, NewRow = noref, RowNum = 16)
joint <- InsertRow(joint, NewRow = age, RowNum = 9)
joint <- InsertRow(joint, NewRow = noref, RowNum = 9)
joint <- InsertRow(joint, NewRow = age, RowNum = 5)
joint <- InsertRow(joint, NewRow = noref, RowNum = 5)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "multimorbidity poisson urban district fixed effects1.csv")
 

 
##rural##

## multimorbidity

multimorbidity <- glm.cluster(formula = multi_morbid_dbl ~ age_grp + wealth_quintile_rurb_lab + educatnames + married + sex + tobacco_smoked + tobacco_smokeless + d_id ,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(multimorbidity), confint(multimorbidity)))
write.csv(aware, "RR multimorbidity poisson rural district fixed effects1.csv")

results_aware <-summary(multimorbidity)
write.csv(results_aware, "p Value multimorbidity poisson rural district fixed effects1.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR multimorbidity poisson rural district fixed effects1.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value multimorbidity poisson rural district fixed effects1.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 multimorbidity"," ")

joint <- InsertRow(joint, NewRow = age, RowNum = 21)
joint <- InsertRow(joint, NewRow = noref, RowNum = 21)
joint <- InsertRow(joint, NewRow = age, RowNum = 16)
joint <- InsertRow(joint, NewRow = noref, RowNum = 16)
joint <- InsertRow(joint, NewRow = age, RowNum = 9)
joint <- InsertRow(joint, NewRow = noref, RowNum = 9)
joint <- InsertRow(joint, NewRow = age, RowNum = 5)
joint <- InsertRow(joint, NewRow = noref, RowNum = 5)
joint <- InsertRow(joint, NewRow = age, RowNum = 2)
joint <- InsertRow(joint, NewRow = noref, RowNum = 2)


 write.csv(joint, "multimorbidity poisson rural district fixed effects1.csv")
 
 
 
 
```




```{r univariable regressions multimorbidity with district-level fixed effects}






#####regression

###make urban rural dataset

dhs_nomiss_urban <- filter(dhs_nomiss, urban==1)
dhs_nomiss_rural <- filter(dhs_nomiss, urban==0)



###univariable regressions multimorbidity URBAN    ##



age <- glm.cluster(formula = multi_morbid_dbl ~ age_grp + d_id,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results multimorbidity univariable reg urban district fixed effects.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value multimorbidity univariable reg urban district fixed effects.csv")

wealth <- glm.cluster(formula = multi_morbid_dbl ~  wealth_quintile_rurb_lab + d_id,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results multimorbidity  univariable reg urban district fixed effects.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value multimorbidity univariable reg urban district fixed effects.csv")

educat <- glm.cluster(formula = multi_morbid_dbl ~  educatnames + d_id,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results multimorbidity univariable reg urban district fixed effects.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value multimorbidity univariable reg urban district fixed effects.csv")

married <- glm.cluster(formula = multi_morbid_dbl ~  married + d_id,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results multimorbidity univariable reg urban district fixed effects.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value multimorbidity univariable reg urban district fixed effects.csv")

sex <- glm.cluster(formula = multi_morbid_dbl ~  sex + d_id,  cluster="psu", data=dhs_nomiss_urban, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results multimorbidity univariable reg urban district fixed effects.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value multimorbidity univariable reg urban district fixed effects.csv")




###univariable regressions multimorbidity rural    ##



age <- glm.cluster(formula = multi_morbid_dbl ~ age_grp + d_id,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results multimorbidity univariable reg rural district fixed effects.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value multimorbidity univariable reg rural district fixed effects.csv")

wealth <- glm.cluster(formula = multi_morbid_dbl ~  wealth_quintile_rurb_lab + d_id,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results multimorbidity  univariable reg rural district fixed effects.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value multimorbidity univariable reg rural district fixed effects.csv")

educat <- glm.cluster(formula = multi_morbid_dbl ~  educatnames + d_id,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results multimorbidity univariable reg rural district fixed effects.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value multimorbidity univariable reg rural district fixed effects.csv")

married <- glm.cluster(formula = multi_morbid_dbl ~  married + d_id,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results multimorbidity univariable reg rural district fixed effects.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value multimorbidity univariable reg rural district fixed effects.csv")

sex <- glm.cluster(formula = multi_morbid_dbl ~  sex + d_id,  cluster="psu", data=dhs_nomiss_rural, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results multimorbidity univariable reg rural district fixed effects.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value multimorbidity univariable reg rural district fixed effects.csv")








```









```{r euler for hiv not used}



    fit2 <- euler(c(HIV = (length(which(dhs_nomiss$hiv03_dbl==1))), Hypertension = (length(which(dhs_nomiss$ex_htn_broad_ind_dbl==1))), Asthma = (length(which(dhs_nomiss$asthma_dbl==1))), Diabetes = (length(which(dhs_nomiss$ex_diab_broad_ind_dbl==1))), Anemia = (length(which(dhs_nomiss$anemia_dbl==1))),
                  "Anemia&HIV"= (length(which(dhs_nomiss$hiv03_dbl==1& dhs_nomiss$anemia_dbl==1))),
             "Diabetes&HIV"= (length(which(dhs_nomiss$hiv03_dbl==1& dhs_nomiss$ex_diab_broad_ind_dbl==1))),
   "Asthma&HIV"= (length(which(dhs_nomiss$asthma_dbl==1& dhs_nomiss$hiv03_dbl==1))),
    "Hypertension&HIV"= (length(which(dhs_nomiss$ex_htn_broad_ind_dbl==1& dhs_nomiss$hiv03_dbl==1)))))
   
   
  plot(fit2,
       fills = c("dodgerblue4", "darkgoldenrod1", "cornsilk4", "firebrick","burlywood" ),
       edges = TRUE,
       fontsize = 8,
       quantities = list(fontsize = 8))
  
   fit2
  

```





```{r forward looping not used}

output <- vector("double", ncol(merg))  # 1. output
for (i in seq_along(merg$anemia_dbl)) {            # 2. sequence
  output[[i]] <- median(merg$anemia_dbl[[i]])      # 3. body
}
output

```



```{r possibly useful not used}

 
 merg$obese <- as.factor(merg$obese)
 merg$daily_smoke <- as.factor(merg$daily_smoke)
 merg$smoke <- as.factor(merg$smoke)
 merg$overweight <- as.factor(merg$overweight)
 merg$ex_diab_broad_ind <- as.factor(merg$ex_diab_broad_ind)
 merg$ex_htn_broad_ind <- as.factor(merg$ex_htn_broad_ind)
 merg$ex_anemia_ind <- as.factor(merg$ex_anemia_ind)
 merg$ex_diab_broad_ind <- as.factor(merg$ex_diab_broad_ind)
 merg$ex_htn_broad_ind <- as.factor(merg$ex_htn_broad_ind)

 
 merg <- mutate(merg, 
              underweight = ifelse(bmi<18.5,1,0))
 
 merg$underweight <- as.factor(merg$underweight)
 
 summary(merg$obese)
 summary(merg$daily_smoke)
 summary(merg$smoke)
summary(merg$overweight)
#summary(merg$bmi)
summary(merg$sev_underweight)
summary(merg$ex_diab_broad_ind)
summary(merg$ex_htn_broad_ind)
summary(merg$ex_anemia_ind)
summary(merg$ex_diab_broad_ind)
summary(merg$ex_htn_broad_ind)

###obese in datasets

merg <- mutate(merg, 
             AHS_obese = ifelse(svy=="AHS" & obese==1, 1, 0))
merg$AHS_obese <- as.factor(merg$AHS_obese)
summary(merg$AHS_obese)

merg <- mutate(merg, 
             DLHS_obese = ifelse(svy=="DLHS" & obese==1, 1, 0))

merg$DLHS_obese <- as.factor(merg$DLHS_obese)
summary(merg$DLHS_obese)

merg <- mutate(merg, 
               DHS_obese = ifelse(svy=="DHS" & obese==1, 1, 0))

merg$DHS_obese <- as.factor(merg$DHS_obese)
summary(merg$DHS_obese)

#####Overweight in Datasets


merg <- mutate(merg, 
               AHS_overweight = ifelse(svy=="AHS" & overweight==1, 1, 0))
merg$AHS_overweight <- as.factor(merg$AHS_overweight)
summary(merg$AHS_overweight)

merg <- mutate(merg, 
               DLHS_overweight = ifelse(svy=="DLHS" & overweight==1, 1, 0))

merg$DLHS_overweight <- as.factor(merg$DLHS_overweight)
summary(merg$DLHS_overweight)

merg <- mutate(merg, 
               DHS_overweight = ifelse(svy=="DHS" & overweight==1, 1, 0))

merg$DHS_overweight <- as.factor(merg$DHS_overweight)
summary(merg$DHS_overweight)


#####smoke in datasets


merg <- mutate(merg, 
               AHS_smoke = ifelse(svy=="AHS" & smoke==1, 1, 0))
merg$AHS_smoke <- as.factor(merg$AHS_smoke)
summary(merg$AHS_smoke)

merg <- mutate(merg, 
               DLHS_smoke = ifelse(svy=="DLHS" & smoke==1, 1, 0))

merg$DLHS_smoke <- as.factor(merg$DLHS_smoke)
summary(merg$DLHS_smoke)

merg <- mutate(merg, 
               DHS_smoke = ifelse(svy=="DHS" & smoke==1, 1, 0))

merg$DHS_smoke <- as.factor(merg$DHS_smoke)
summary(merg$DHS_smoke)

####underweight in datasets

merg <- mutate(merg, 
               AHS_underweight = ifelse(svy=="AHS" & underweight==1, 1, 0))
merg$AHS_underweight <- as.factor(merg$AHS_underweight)
summary(merg$AHS_underweight)

merg <- mutate(merg, 
               DLHS_underweight = ifelse(svy=="DLHS" & underweight==1, 1, 0))

merg$DLHS_underweight <- as.factor(merg$DLHS_underweight)
summary(merg$DLHS_underweight)

merg <- mutate(merg, 
               DHS_underweight = ifelse(svy=="DHS" & underweight==1, 1, 0))

merg$DHS_underweight <- as.factor(merg$DHS_underweight)
summary(merg$DHS_underweight)


#####ex_diab_broad_ind in datasets

merg <- mutate(merg, 
               AHS_ex_diab_broad_ind = ifelse(svy=="AHS" & ex_diab_broad_ind==1, 1, 0))
merg$AHS_ex_diab_broad_ind <- as.factor(merg$AHS_ex_diab_broad_ind)
summary(merg$AHS_ex_diab_broad_ind)

merg <- mutate(merg, 
               DLHS_ex_diab_broad_ind = ifelse(svy=="DLHS" & ex_diab_broad_ind==1, 1, 0))

merg$DLHS_ex_diab_broad_ind <- as.factor(merg$DLHS_ex_diab_broad_ind)
summary(merg$DLHS_ex_diab_broad_ind)

merg <- mutate(merg, 
               DHS_ex_diab_broad_ind = ifelse(svy=="DHS" & ex_diab_broad_ind==1, 1, 0))

merg$DHS_ex_diab_broad_ind <- as.factor(merg$DHS_ex_diab_broad_ind)
summary(merg$DHS_ex_diab_broad_ind)

#####htn in datasets

merg <- mutate(merg, 
               AHS_ex_htn_broad_ind = ifelse(svy=="AHS" & ex_htn_broad_ind==1, 1, 0))
merg$AHS_ex_htn_broad_ind <- as.factor(merg$AHS_ex_htn_broad_ind)
summary(merg$AHS_ex_htn_broad_ind)

merg <- mutate(merg, 
               DLHS_ex_htn_broad_ind = ifelse(svy=="DLHS" & ex_htn_broad_ind==1, 1, 0))

merg$DLHS_ex_htn_broad_ind <- as.factor(merg$DLHS_ex_htn_broad_ind)
summary(merg$DLHS_ex_htn_broad_ind)

merg <- mutate(merg, 
               DHS_ex_htn_broad_ind = ifelse(svy=="DHS" & ex_htn_broad_ind==1, 1, 0))

merg$DHS_ex_htn_broad_ind <- as.factor(merg$DHS_ex_htn_broad_ind)
summary(merg$DHS_ex_htn_broad_ind)


#####anemia in datasets


merg <- mutate(merg, 
               AHS_ex_hb_adj_ind = ifelse(svy=="AHS" & ex_hb_adj_ind==1, 1, 0))
merg$AHS_ex_hb_adj_ind <- as.factor(merg$AHS_ex_hb_adj_ind)
summary(merg$AHS_ex_hb_adj_ind)

merg <- mutate(merg, 
               DLHS_ex_hb_adj_ind = ifelse(svy=="DLHS" & ex_hb_adj_ind==1, 1, 0))

merg$DLHS_ex_hb_adj_ind <- as.factor(merg$DLHS_ex_hb_adj_ind)
summary(merg$DLHS_ex_hb_adj_ind)

merg <- mutate(merg, 
               DHS_ex_hb_adj_ind = ifelse(svy=="DHS" & ex_hb_adj_ind==1, 1, 0))

merg$DHS_ex_hb_adj_ind <- as.factor(merg$DHS_ex_hb_adj_ind)
summary(merg$DHS_ex_hb_adj_ind)



dhs <- dplyr::filter(merg, svy == "DHS")
dlhs <- dplyr::filter(merg, svy == "DLHS")
ahs <- dplyr::filter(merg, svy == "AHS")

dhs$obese <- as.factor(dhs$obese)
dhs$smoke <- as.factor(dhs$smoke)
dhs$overweight <- as.factor(dhs$overweight)
dhs$underweight <- as.factor(dhs$underweight)
dhs$ex_anemia_ind <- as.factor(dhs$ex_hb_adj_ind)
dhs$ex_diab_broad_ind <- as.factor(dhs$ex_diab_broad_ind)
dhs$ex_htn_broad_ind <- as.factor(dhs$ex_htn_broad_ind)

dlhs$obese <- as.factor(dlhs$obese)
dlhs$smoke <- as.factor(dlhs$smoke)
dlhs$overweight <- as.factor(dlhs$overweight)
dlhs$underweight <- as.factor(dlhs$underweight)
dlhs$ex_anemia_ind <- as.factor(dlhs$ex_anemia_ind)
dlhs$ex_diab_broad_ind <- as.factor(dlhs$ex_diab_broad_ind)
dlhs$ex_htn_broad_ind <- as.factor(dlhs$ex_htn_broad_ind)

ahs$obese <- as.factor(ahs$obese)
ahs$smoke <- as.factor(ahs$smoke)
ahs$overweight <- as.factor(ahs$overweight)
ahs$underweight <- as.factor(ahs$underweight)
ahs$ex_anemia_ind <- as.factor(ahs$ex_anemia_ind)
ahs$ex_diab_broad_ind <- as.factor(ahs$ex_diab_broad_ind)
ahs$ex_htn_broad_ind <- as.factor(ahs$ex_htn_broad_ind)

summary(dhs$obese)
summary(dhs$smoke)
summary(dhs$overweight)
summary(dhs$underweight)
summary(dhs$ex_hb_adj_ind)
summary(dhs$ex_diab_broad_ind)
summary(dhs$ex_htn_broad_ind)

summary(dlhs$obese)
summary(dlhs$smoke)
summary(dlhs$overweight)
summary(dlhs$underweight)
summary(dlhs$ex_anemia_ind)
summary(dlhs$ex_diab_broad_ind)
summary(dlhs$ex_htn_broad_ind)

summary(ahs$obese)
summary(ahs$smoke)
summary(ahs$overweight)
summary(ahs$underweight)
summary(ahs$ex_anemia_ind)
summary(ahs$ex_diab_broad_ind)
summary(ahs$ex_htn_broad_ind)


merg <- mutate(merg, 
             AHS_overweight = ifelse(svy=="AHS" & overweight==1, 1, 0))
merg$AHS_overweight <- as.factor(merg$AHS_overweight)
summary(merg$AHS_overweight)

merg <- mutate(merg, 
             DLHS_overweight = ifelse(svy=="DLHS" & overweight==1, 1, 0))
merg$DLHS_overweight <- as.factor(merg$DLHS_overweight)
summary(merg$DLHS_overweight)

merg$all_disl <- as.factor(merg$all_disl)
summary(merg$all_disl)

merg <- mutate(merg, 
             AHS_all_disl = ifelse(svy=="AHS" & all_disl==1, 1, 0))
merg$AHS_all_disl <- as.factor(merg$AHS_all_disl)
summary(merg$AHS_all_disl)

merg <- mutate(merg, 
             DLHS_all_disl = ifelse(svy=="DLHS" & all_disl==1, 1, 0))
merg$DLHS_all_disl <- as.factor(merg$DLHS_all_disl)
summary(merg$DLHS_all_disl)

merg$hchol12 <- as.factor(merg$hchol12)
summary(merg$hchol12)

merg <- mutate(merg, 
             AHS_hchol12 = ifelse(svy=="AHS" & hchol12==1, 1, 0))
merg$AHS_hchol12 <- as.factor(merg$AHS_hchol12)
summary(merg$AHS_hchol12)

merg <- mutate(merg, 
             DLHS_hchol12 = ifelse(svy=="DLHS" & hchol12==1, 1, 0))
merg$DLHS_hchol12 <- as.factor(merg$DLHS_hchol12)
summary(merg$DLHS_hchol12)

summary(merg$triglyc_mgdl)
summary(merg$hdl_mgdl)

merg$ex_anemia_ind <- as.factor(merg$ex_anemia_ind)
summary(merg$ex_anemia_ind)

merg <- mutate(merg, 
             AHS_ex_anemia_ind = ifelse(svy=="AHS" & ex_anemia_ind==1, 1, 0))
merg$AHS_ex_anemia_ind <- as.factor(merg$AHS_ex_anemia_ind)
summary(merg$AHS_ex_anemia_ind)

merg <- mutate(merg, 
             DLHS_ex_anemia_ind = ifelse(svy=="DLHS" & ex_anemia_ind==1, 1, 0))
merg$DLHS_ex_anemia_ind <- as.factor(merg$DLHS_ex_anemia_ind)
summary(merg$DLHS_ex_anemia_ind)



 
 
 install.packages("UpSetR")








Men <- read_dta("~/Documents/Public Health Files/Public Health/Multiple Morbidities/Datasets/IAMR72DT/IAMR72FL.DTA")

Women <- read_dta("~/Documents/Public Health Files/Public Health/Multiple Morbidities/Datasets/IAIR72DT/IAIR72FL.DTA")

HIV <- read_dta("~/Documents/Public Health Files/Public Health/Multiple Morbidities/Datasets/HIV dhs/IAAR71FL.DTA")


AD <- read_dta("~/Documents/Public Health Files/Public Health/Multiple Morbidities/Datasets/DLHS-4 & AHS.DTA")


Men$sm626d <- as.factor(Men$sm626d)
summary(Men$sm626d)



colnames(AD)

AD$obese <- as.factor(AD$obese)
summary(AD$obese)


AD <- mutate(AD, 
              AHS_obese = ifelse(svy=="AHS" & obese==1, 1, 0))
AD$AHS_obese <- as.factor(AD$AHS_obese)
summary(AD$AHS_obese)

AD <- mutate(AD, 
             DLHS_obese = ifelse(svy=="DLHS" & obese==1, 1, 0))

AD$overweight <- as.factor(AD$overweight)
summary(AD$overweight)

AD <- mutate(AD, 
             AHS_overweight = ifelse(svy=="AHS" & overweight==1, 1, 0))
AD$AHS_overweight <- as.factor(AD$AHS_overweight)
summary(AD$AHS_overweight)

AD <- mutate(AD, 
             DLHS_overweight = ifelse(svy=="DLHS" & overweight==1, 1, 0))
AD$DLHS_overweight <- as.factor(AD$DLHS_overweight)
summary(AD$DLHS_overweight)

AD$all_disl <- as.factor(AD$all_disl)
summary(AD$all_disl)

AD <- mutate(AD, 
             AHS_all_disl = ifelse(svy=="AHS" & all_disl==1, 1, 0))
AD$AHS_all_disl <- as.factor(AD$AHS_all_disl)
summary(AD$AHS_all_disl)

AD <- mutate(AD, 
             DLHS_all_disl = ifelse(svy=="DLHS" & all_disl==1, 1, 0))
AD$DLHS_all_disl <- as.factor(AD$DLHS_all_disl)
summary(AD$DLHS_all_disl)

AD$hchol12 <- as.factor(AD$hchol12)
summary(AD$hchol12)

AD <- mutate(AD, 
             AHS_hchol12 = ifelse(svy=="AHS" & hchol12==1, 1, 0))
AD$AHS_hchol12 <- as.factor(AD$AHS_hchol12)
summary(AD$AHS_hchol12)

AD <- mutate(AD, 
             DLHS_hchol12 = ifelse(svy=="DLHS" & hchol12==1, 1, 0))
AD$DLHS_hchol12 <- as.factor(AD$DLHS_hchol12)
summary(AD$DLHS_hchol12)

summary(AD$triglyc_mgdl)
summary(AD$hdl_mgdl)

AD$ex_anemia_ind <- as.factor(AD$ex_anemia_ind)
summary(AD$ex_anemia_ind)

AD <- mutate(AD, 
             AHS_ex_anemia_ind = ifelse(svy=="AHS" & ex_anemia_ind==1, 1, 0))
AD$AHS_ex_anemia_ind <- as.factor(AD$AHS_ex_anemia_ind)
summary(AD$AHS_ex_anemia_ind)

AD <- mutate(AD, 
             DLHS_ex_anemia_ind = ifelse(svy=="DLHS" & ex_anemia_ind==1, 1, 0))
AD$DLHS_ex_anemia_ind <- as.factor(AD$DLHS_ex_anemia_ind)
summary(AD$DLHS_ex_anemia_ind)






AD$obese <- as.factor(AD$obese)
summary(AD$overweight)


read.dta(AHS)
read_dta

AD4 <- HPACC_final_big

setwd("~/Documents/Public Health Files/Public Health/Multiple Morbidities")

View(AD4$hv23)

AD4$hv23 <- as.factor(AD4$hv23)

diseases <- summary(AD4$hv23)
write.table(diseases)
write.csv(diseases, "diseases in AHS DLHS 3.0.csv")


Variab <-colnames(AD4)
write.table(Variab)
write.csv(Variab, "Variables in DLHS and AHS.csv")


View(AD4$svy)
AD4$svy <- as.factor(AD4$svy)
summary(AD4$svy)

AD4 <- mutate(AD4, 
              NA_in_AHS = ifelse(svy=="AHS" & is.na(hv23)==F, 1, 0))

AD4$NA_in_AHS <- as.factor(AD4$NA_in_AHS)
summary(AD4$NA_in_AHS)


sum(AD4$p_id==133097900008003)

AD4$p_id <- as.factor(AD4$p_id)
sum_p_id<-summary(AD4$p_id)
write.table(sum_p_id)
write.csv(sum_p_id, "doubles in pid.csv")

summary(dhs$all_disl)
dhsnames <-colnames(dhs)
write.table(dhsnames)



# Now calculate prevalence by state
temp.dat2 <- merg %>% 
  group_by(ex_state_ind) %>%
  mutate(multi_risc = 100*weighted.mean(csmkls_tb_dbl,sworld_weight_india, na.rm=TRUE)) %>% 
  filter(row_number()==1) %>% 
  dplyr::select(ex_state_ind, multi_risc) %>% 
  filter(ex_state_ind!="Daman and Diu")  # Daman and Diu has a crazy high urban prev, so to not distort color scale kick out this invisible state





```{r DHS Input}


DHS.with.HIV.smoke.smokeless.asthma.heart.thyroid.cancer.correct <- read.csv("~/iCloud Drive (Archive)/Documents/Public Health Files/Public Health/Multiple Morbidities/Datasets/DHS with HIV smoke smokeless asthma heart thyroid cancer correct.csv")


dhs <- DHS.with.HIV.smoke.smokeless.asthma.heart.thyroid.cancer.correct
dhs <- as_tibble(dhs)

setwd("~/Documents/Public Health Files/Public Health/public health/hypertension/Hypertension with screened/with changed prev/1 year weights")

dhs <- mutate(dhs,
                hypt = ifelse(hypt==1 & bp_ms==0, NA, hypt))
dhs <- mutate(dhs,
              hypt_med = ifelse(hypt_med==1 & bp_ms==0, NA, hypt_med))



dhs <-mutate(dhs,
             ex_htn_broad_ind = ifelse(is.na(ex_htn_broad_ind)==T | is.na(hypt) ==T | is.na(hypt_med)==T, NA,ifelse(hypt_med==1 | hypt==1 | ex_htn_broad_ind==1, 1, 0)))
 
dhs <- mutate(dhs, p_wt_new = ifelse(sex==0 & age==15, p_wt* { ( (25101) / (12159708) )   /   ( (3761) / (13739746) ) },
                                     ifelse(sex==0 & age==16,  p_wt* { ( (24702) / (11564358) )   /   ( (3844) / (13027935) ) },
                                            ifelse(sex==0 & age==17,  p_wt* { ( (22674) / (9868018) )   /   ( (3725) / (11349449) ) },
                                                   ifelse(sex==0 & age==18,   p_wt* { ( (25320) / (12937296) )   /   ( (4177) / (15020851) ) },
                                                          ifelse(sex==0 & age==19,   p_wt* { ( (19462) / (10014673) )   /   ( (3203) / (10844415) ) },
                                                                 ifelse(sex==0 & age==20,   p_wt* { ( (23573) / (13990570) )   /   ( (3632) / (14892165) ) },
                                                                        ifelse(sex==0 & age==21,  p_wt* { ( (18442) / (9446694) )   /   ( (3028) / (10532278) ) },
                                                                               ifelse(sex==0 & age==22,   p_wt* { ( (22212) / (11135249) )   /   ( (3472) / (12392976) ) },
                                                                                      ifelse(sex==0 & age==23,   p_wt* { ( (19660) / (9479866) )   /   ( (2989) / (9674189) ) },
                                                                                             ifelse(sex==0 & age==24,   p_wt* { ( (19262) / (9787150) )   /   ( (3061) / (10093085) ) },
                                                                                                    ifelse(sex==0 & age==25,  p_wt* { ( (25318) / (13456554) )   /   ( (3821) / (14311524) ) },
                                                                                                           ifelse(sex==0 & age==26,   p_wt* { ( (19390) / (9761967) )   /   ( (3131) / (10315030) ) },
                                                                                                                  ifelse(sex==0 & age==27,   p_wt* { ( (18113) / (8157318) )   /   ( (2957) / (8552032) ) },
                                                                                                                         ifelse(sex==0 & age==28,   p_wt* { ( (22299) / (11407090) )   /   ( (3413) / (10719926) ) },
                                                                                                                                ifelse(sex==0 & age==29,   p_wt* { ( (15413) / (7286828) )   /   ( (2476) / (7445696) ) },
                                                                                                                                       ifelse(sex==0 & age==30,  p_wt* { ( (27923) / (14770033) )   /   ( (4356) / (15628996) ) },
                                                                                                                                              ifelse(sex==0 & age==31,   p_wt* { ( 13757 / (6665743) )   /   ( (2190) / (7157502) ) },
                                                                                                                                                     ifelse(sex==0 & age==32, p_wt* { ( (20375) / (8812439) )   /   ( (3156) / (8801105) ) },
                                                                                                                                                            ifelse(sex==0 & age==33,  p_wt* { ( (14514) / (6655662) )   /   ( (2299) / (6108879) ) },
                                                                                                                                                                   ifelse(sex==0 & age==34,   p_wt* { ( (14285) / (7030400) )   /   ( (2348) / (6964192) ) },
                                                                                                                                                                          ifelse(sex==0 & age==35,   p_wt* { ( (27035) / (13385965) )   /   ( (4391) / (15036666) ) },
                                                                                                                                                                                 ifelse(sex==0 & age==36,   p_wt* { ( (15670) / (7760149) )   /   ( (2435) / (8067568) ) },
                                                                                                                                                                                        ifelse(sex==0 & age==37,   p_wt* { ( (13795) / (5907352) )   /   ( (2176) / (5784879) ) },
                                                                                                                                                                                               ifelse(sex==0 & age==38,   p_wt* { ( (18693) / (9381357) )   /   ( (2723) / (8090401) ) },
                                                                                                                                                                                                      ifelse(sex==0 & age==39,   p_wt* { ( (12683) / (5786480) )   /   ( (1968) / (5939867) ) },
                                                                                                                                                                                                             ifelse(sex==0 & age==40,   p_wt* { ( (25682) / (13355581) )   /   ( (3974) / (15173411) ) },
                                                                                                                                                                                                                    ifelse(sex==0 & age==41,  p_wt* { ( (11046) / (5395597) )   /   ( (1819) / (6172297) ) },
                                                                                                                                                                                                                           ifelse(sex==0 & age==42,  p_wt* { ( (16124) / (6523816) )   /   ( (2544) / (6856826) ) },
                                                                                                                                                                                                                                  ifelse(sex==0 & age==43,   p_wt* { ( (11983) / (4865438) )   /   ( (1797) / (4468914) ) },
                                                                                                                                                                                                                                         ifelse(sex==0 & age==44,   p_wt* { ( (10836) / (4752294) )   /   ( (1714) / (4873938) ) },
                                                                                                                                                                                                                                                ifelse(sex==0 & age==45,  p_wt* { ( (23877) / (11187786) )   /   ( (3854) / (12685175) ) },
                                                                                                                                                                                                                                                       ifelse(sex==0 & age==46,  p_wt* { ( (11752) / (5257138) )   /   ( (1895) / (5735540) ) },
                                                                                                                                                                                                                                                              ifelse(sex==0 & age==47,   p_wt* { ( (11295) / (3908175) )   /   ( (1714) / (4043122) ) },
                                                                                                                                                                                                                                                                     ifelse(sex==0 & age==48, p_wt* { ( (14786) / (6081038) )   /   ( (2119) / (5568554) ) },
                                                                                                                                                                                                                                                                            ifelse(sex==0 & age==49,   p_wt* { ( (10399) / (3746076) )   /   ( (1506) / (4105723)) }, p_wt ))))))))))))))))))))))))))))))))))))
dhs <- mutate(dhs, p_wt_new = ifelse(sex==0 & age>=50, p_wt_new/1.013194, p_wt_new )) 
dhs <- mutate(dhs, p_wt_new = ifelse(sex==0 & age>=50, p_wt * { ((6.72342) * (1.013194)) / (0.998047) }, p_wt_new ))   




####new anemia variable

dhs <- dplyr::mutate(dhs,
                           mild_anemia = ifelse((sex = 1 & ex_hb_ind < 12 & ex_hb_ind >= 11) | (sex = 0 & ex_hb_ind < 13 &ex_hb_ind >= 11 ), 1, 0))
dhs$mild_anemia <- factor(dhs$mild_anemia, levels = c("0", "1"))

dhs <- dplyr::mutate(dhs,
                            moderate_anemia = ifelse((sex = 1 & ex_hb_ind < 11 & ex_hb_ind >= 8) | (sex = 0 & ex_hb_ind < 11 & ex_hb_ind >= 8 ), 1, 0))
dhs$moderate_anemia <- factor(dhs$moderate_anemia, levels = c("0", "1"))

dhs <- dplyr::mutate(dhs,
                            severe_anemia = ifelse((sex = 1 & ex_hb_ind < 8) | (sex = 0 & ex_hb_ind < 8 ), 1, 0))
dhs$severe_anemia <- factor(dhs$severe_anemia, levels = c("0", "1")) 


dhs <- mutate(dhs, 
               ex_anemia_ind = ifelse(moderate_anemia==1 | severe_anemia==1,1,0))

length(which(dhs$ex_anemia_ind==1))

#####create correct morbiditiy categories
dhs <- mutate(dhs, 
               sev_underweight = ifelse(bmi<16,1,0))

dhs <- mutate(dhs, 
               obese = ifelse(bmi>=27.5,1,0))

######make new smoking variable since csmoke is incorrect for dhs

dhs <- mutate(dhs, 
                     smoke = ifelse(tobacco_smoked==1,1,0),
                                    csmkls_tb = ifelse(tobacco_smokeless==1,1,0))




# 3. Filter out those <18 or pregnant  #
#dhs_nomiss <- dplyr::filter(dhs_nomiss, age> 18) # only those >18 and with non-missing age
dhs <- dplyr::filter(dhs, pregnant == 0)  # only keep those who aren't pregnant (they didn't measure glucose in pregnant women anyway); dhs_nomiss has 

######FILTER out those that have missing values for morbidities


dhs <- filter(dhs, is.na(dhs$obese)==F & is.na(dhs$sev_underweight)==F & is.na(dhs$ex_diab_broad_ind)==F & is.na(dhs$ex_htn_broad_ind)==F)

#####create correct characteristics

dhs <- dplyr::mutate(dhs, age_grp = ifelse(age<=24 , "15-24", 
                                                             ifelse(age>24 &  age<=34, "25-34",
                                                                    ifelse(age>34 &  age<=44, "35-44",
                                                                           ifelse(age>44 &  age<=54, "45-54", 
                                                                                  ifelse(age>54 &  age<=64, "55-64",
                                                                                         ifelse(age>64, "65+", NA)))))))

                                                                              
dhs$age_grp <- factor(dhs$age_grp, levels = c("15-24", "25-34", "35-44", "45-54","55-64", "65+"))
dhs <- within(dhs, age_grp <- relevel(age_grp, ref = "15-24"))


dhs <- dplyr::mutate(dhs, age_grp_old = ifelse(age<=25 , "15-25", 
                                                             ifelse(age>25 &  age<=35, "26-35",
                                                                    ifelse(age>35 &  age<=45, "36-45",
                                                                           ifelse(age>45 &  age<=55, "46-55", 
                                                                                  ifelse(age>55 &  age<=65, "56-65",
                                                                                         ifelse(age>65 &  age<=75, "66-75",
                                                                                         ifelse(age>75, "76+", NA))))))))

                                                                              
dhs$age_grp_old <- factor(dhs$age_grp_old, levels = c("15-25", "26-35", "36-45", "46-55","56-65", "66-75", "76+"))
dhs <- within(dhs, age_grp_old <- relevel(age_grp_old, ref = "15-25"))



dhs <- mutate(dhs, 
                     rural = ifelse(urban==1, 0, 1),
                     female = ifelse(sex==1, 1, 0),
                     male = ifelse(sex==1, 0, 1))
dhs$rural <- as.factor(dhs$rural)
dhs$female <- as.factor(dhs$female)
dhs$male <- as.factor(dhs$male)

dhs <- dplyr::mutate(dhs, educatnames = ifelse(educat_lcl==0, "No formal education", 
                                                             ifelse(educat_lcl==1, "< Primary school",
                                                                    ifelse(educat_lcl==2, "Primary school",
                                                                           ifelse(educat_lcl==3, "Middle school",
                                                                                  ifelse(educat_lcl==4, "Secondary school",
                                                                                          ifelse(educat_lcl==5, "> Secondary school",NA))))))) 
dhs$educatnames <- factor(dhs$educatnames, levels = c("No formal education", "< Primary school", "Primary school", "Middle school", "Secondary school", "> Secondary school", NA))
dhs <- within(dhs, educatnames<- relevel(educatnames, ref = "No formal education"))


dhs <- dplyr::mutate(dhs, educatnames_few = ifelse(educat_lcl==0 | educat_lcl==1, "< Primary school", 
                                                            ifelse(educat_lcl==2 | educat_lcl==3, "< Secondary school",
                                                                           ifelse(educat_lcl==4, "Secondary school",
                                                                                  ifelse(educat_lcl==5, "> Secondary school",NA))))) 
dhs$educatnames_few <- factor(dhs$educatnames_few, levels = c("< Primary school", "< Secondary school", "Secondary school", "> Secondary school", NA))
dhs <- within(dhs, educatnames_few<- relevel(educatnames_few, ref = "< Primary school"))




dhs <- dhs %>% 
  mutate(female_lab = as.factor(ifelse(is.na(female) == TRUE, NA, 
                                       ifelse(female == 1, "Female", "Male"))),
         urban_lab = as.factor(ifelse(is.na(rural) == TRUE, NA, 
                                      ifelse(rural == 1, "Rural", "Urban"))),
         wealth_quintile_rurb_lab = as.factor(ifelse(is.na(wealth_quintile_rurb) == TRUE, NA, 
                                                     ifelse( wealth_quintile_rurb == 1, "Q1 (Poorest)", 
                                                             ifelse(wealth_quintile_rurb == 2, "Q2",
                                                                    ifelse(wealth_quintile_rurb == 3, "Q3",
                                                                           ifelse(wealth_quintile_rurb == 4, "Q4",
                                                                                  ifelse(wealth_quintile_rurb == 5, "Q5 (Richest)", NA))))))))

dhs$wealth_quintile_rurb_lab <- as.factor(dhs$wealth_quintile_rurb_lab)
dhs<- within(dhs, wealth_quintile_rurb_lab <- relevel(wealth_quintile_rurb_lab, ref = "Q1 (Poorest)"))

dhs$obese <- as.factor(dhs$obese)
dhs$csmoke <- as.factor(dhs$csmoke)
dhs$ex_htn_broad_ind <- as.factor(dhs$ex_htn_broad_ind)
dhs$ex_diab_broad_ind <- as.factor(dhs$ex_diab_broad_ind)
dhs$sev_underweight <- as.factor(dhs$sev_underweight)
dhs$ex_anemia_ind <- as.factor(dhs$ex_anemia_ind)
dhs$educat <- as.factor(dhs$educat)






#######make numeric morbidities where NA is 0

dhs <- mutate(dhs, 
               anemia = ifelse(ex_anemia_ind==1,1,0))
dhs <- mutate(dhs, 
               csmkls_tb = ifelse(csmkls_tb==1,1,0))
dhs <- mutate(dhs, 
              anemia_dbl = as.numeric(anemia),
              obese_dbl = as.numeric(obese)-1,
              sev_underweight_dbl = as.numeric(sev_underweight)-1,
              ex_diab_broad_ind_dbl = as.numeric(ex_diab_broad_ind)-1,
              ex_htn_broad_ind_dbl = as.numeric(ex_htn_broad_ind)-1,
              smoke_dbl = as.numeric(smoke),
              csmkls_tb_dbl = as.numeric(csmkls_tb))

summary(dhs$obese_dbl)
summary(dhs$sev_underweight_dbl)
summary(dhs$ex_diab_broad_ind_dbl)
summary(dhs$ex_htn_broad_ind_dbl)
summary(dhs$smoke_dbl)
summary(dhs$csmkls_tb_dbl)
summary(dhs$asthma)


#####make NAs to 0 in dbl

dhs[which(is.na(dhs$anemia_dbl)==T), "anemia_dbl"]<-0

dhs[which(is.na(dhs$smoke_dbl)==T), "smoke_dbl"]<-0

dhs[which(is.na(dhs$ex_diab_broad_ind_dbl)==T), "ex_diab_broad_ind_dbl"]<-0
dhs[which(is.na(dhs$ex_htn_broad_ind_dbl)==T), "ex_htn_broad_ind_dbl"]<-0
dhs[which(is.na(dhs$obese_dbl)==T), "obese_dbl"]<-0
dhs[which(is.na(dhs$sev_underweight_dbl)==T), "sev_underweight_dbl"]<-0
dhs[which(is.na(dhs$csmkls_tb_dbl)==T), "csmkls_tb_dbl"]<-0
dhs[which(is.na(dhs$asthma)==T), "asthma"]<-0




 ######make morbidity Dummy                       

dhs<- mutate(dhs, 
              sum_multi = anemia_dbl  + ex_diab_broad_ind_dbl + ex_htn_broad_ind_dbl + asthma + obese_dbl)
               
dhs <- mutate(dhs,
               multi_morbid = ifelse(sum_multi>=2,1,0))

dhs <- mutate(dhs, 
               multi_morbid_dbl = as.numeric(multi_morbid))

dhs$multi_morbid <- as.factor(dhs$multi_morbid)
dhs$sum_multi <- as.factor(dhs$sum_multi)

summary(dhs$multi_morbid)
summary(dhs$sum_multi)


dhs <-  mutate(dhs, 
              sum_multi_dbl = as.numeric(sum_multi)-1)
               


#dhse age standardization weight from GBD India pop into the dataset
dhs <-dhs %>%
  mutate(age_5yr_2=ifelse(age>=65& age<=69,11,
                                         ifelse(age>=70&age<=74,12,
                                                ifelse(age>=75 &age<=79,13,
                                                      ifelse( age>=80,14,age_5yr)))))

GBDpopweights_2018.04.24.age_grp15.19 <- read.csv("~/iCloud Drive (Archive)/Documents/Public Health Files/Public Health/Multiple Morbidities/Datasets/GBDpopweights_2018-04-24-age_grp15-19.csv")

agest_india <- GBDpopweights_2018.04.24.age_grp15.19
agest_india$sex <-as.factor(agest_india$sex)
dhs$sex <- as.factor(dhs$sex)

dhs <- left_join(dhs, agest_india) 
dhs <- mutate(dhs, 
                    sworld_weight_india = p_wt*gbd_weight)

dhs <- mutate(dhs, 
                    sworld_weight_india = ifelse(is.na(gbd_weight)==TRUE, mean(sworld_weight_india, na.rm=TRUE), sworld_weight_india))




dhs <- mutate( dhs,
                      married = ifelse( dhs$marital==2, 1, 0))

dhs$married <- as.factor(dhs$married)


dhs <- mutate( dhs,
                      marriednames = ifelse( dhs$marital==2, "Married","Not married"))

dhs$marriednames <- as.factor(dhs$marriednames)

dhs <- within(dhs, marriednames<- relevel(marriednames, ref = "Not married"))








```
